Qt5官方demo解析集30——Extending QML - Binding Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873

接上文Qt5官方demo解析集29——Extending
QML - Property Value Source Example

还记得我们曾经在Qt5官方demo解析集17——Chapter
3: Adding Property Bindings
一文中接触过QML自定义类型的属性绑定吗?如果不记得了,可以移步进行了解。因为项目尺寸的原因,那个例子可能更好理解。

这个例子也是我们Extending QML(扩展QML)系列的最后一个例子了,虽然相较前一个例子也只有小小的改动,不过我们还是把整个工程都完整的看一遍吧~

binding.qrc中是我们的qml文件,它实例化了BirthdayParty类以及其所有的子对象。

Person类建立了一个自定义的QML类型,由于它并不是一个可视化组件,且QML任何组件均基于Qt 的元对象系统,因此继承自QObject。

接着定义了ShoeDescription用来对Person类的shoe属性进行描述,使用特定的方法,我们在对shoe赋值时不需要实例化这个ShoeDescription组件。

再定义两个Person的派生类Boy、Girl,可以用来对Person对象分类。

person.h:

#ifndef PERSON_H
#define PERSON_H

#include <QObject>
#include <QColor>

class ShoeDescription : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged)        // NOTIFY用在属性绑定,当该属性值发生改变时发出信号shoeChanged
    Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY shoeChanged)  // 通过该信号,我们就能使得被绑定的属性值随之发生改变
    Q_PROPERTY(QString brand READ brand WRITE setBrand NOTIFY shoeChanged)
    Q_PROPERTY(qreal price READ price WRITE setPrice NOTIFY shoeChanged)
public:
    ShoeDescription(QObject *parent = 0);

    int size() const;
    void setSize(int);

    QColor color() const;
    void setColor(const QColor &);

    QString brand() const;
    void setBrand(const QString &);

    qreal price() const;
    void setPrice(qreal);
signals:
    void shoeChanged();                      // 定义该shoeChanged()信号

private:
    int m_size;
    QColor m_color;
    QString m_brand;
    qreal m_price;
};

class Person : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
// ![0]
    Q_PROPERTY(ShoeDescription *shoe READ shoe CONSTANT)
// ![0]
public:
    Person(QObject *parent = 0);

    QString name() const;
    void setName(const QString &);

    ShoeDescription *shoe();
signals:
    void nameChanged();

private:
    QString m_name;
    ShoeDescription m_shoe;
};

class Boy : public Person
{
    Q_OBJECT
public:
    Boy(QObject * parent = 0);
};

class Girl : public Person
{
    Q_OBJECT
public:
    Girl(QObject * parent = 0);
};

#endif // PERSON_H

person.cpp:

#include "person.h"

ShoeDescription::ShoeDescription(QObject *parent)
: QObject(parent), m_size(0), m_price(0)
{
}

int ShoeDescription::size() const
{
    return m_size;
}

void ShoeDescription::setSize(int s)
{
    if (m_size == s)
        return;

    m_size = s;
    emit shoeChanged();                   // 该信号应该在该属性被正确写入后发出
}

QColor ShoeDescription::color() const
{
    return m_color;
}

void ShoeDescription::setColor(const QColor &c)
{
    if (m_color == c)
        return;

    m_color = c;
    emit shoeChanged();
}

QString ShoeDescription::brand() const
{
    return m_brand;
}

void ShoeDescription::setBrand(const QString &b)
{
    if (m_brand == b)
        return;

    m_brand = b;
    emit shoeChanged();
}

qreal ShoeDescription::price() const
{
    return m_price;
}

void ShoeDescription::setPrice(qreal p)
{
    if (m_price == p)
        return;

    m_price = p;
    emit shoeChanged();
}

Person::Person(QObject *parent)
: QObject(parent)
{
}

QString Person::name() const
{
    return m_name;
}

void Person::setName(const QString &n)
{
    if (m_name == n)
        return;

    m_name = n;
    emit nameChanged();
}

ShoeDescription *Person::shoe()
{
    return &m_shoe;
}

Boy::Boy(QObject * parent)
: Person(parent)
{
}

Girl::Girl(QObject * parent)
: Person(parent)
{
}

接下来是我们的主类BirthdayParty,它也是example.qml中的根项目。它有一个以Person指针为参数的host属性,用来指明寿星;有一个以Person列表指针为参数guests属性,用来指明客人,并且该属性被设置为默认属性,这样在QML中没有指明属性的值将被划归它的名下;一个announcement属性,用来被动态改变以播放歌词。另外,该类还定义了一个partyStarted()信号,我们可以在QML中使用onPartyStarted
来响应该信号。

此外,再定义一个BirthdayPartyAttached类,它用来为BirthdayParty提供一个附加属性。

birthdayparty.h:

#ifndef BIRTHDAYPARTY_H
#define BIRTHDAYPARTY_H

#include <QObject>
#include <QDate>
#include <QDebug>
#include <qqml.h>
#include "person.h"

class BirthdayPartyAttached : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp NOTIFY rsvpChanged)  // 该例中大多数属性均定义了属性绑定
public:
    BirthdayPartyAttached(QObject *object);

    QDate rsvp() const;
    void setRsvp(const QDate &);

signals:
    void rsvpChanged();

private:
    QDate m_rsvp;
};

class BirthdayParty : public QObject
{
    Q_OBJECT
// ![0]
    Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged)
// ![0]
    Q_PROPERTY(QQmlListProperty<Person> guests READ guests)
    Q_PROPERTY(QString announcement READ announcement WRITE setAnnouncement)
    Q_CLASSINFO("DefaultProperty", "guests")
public:
    BirthdayParty(QObject *parent = 0);

    Person *host() const;
    void setHost(Person *);

    QQmlListProperty<Person> guests();
    int guestCount() const;
    Person *guest(int) const;

    QString announcement() const;
    void setAnnouncement(const QString &);

    static BirthdayPartyAttached *qmlAttachedProperties(QObject *);

    void startParty();
signals:
    void partyStarted(const QTime &time);
    void hostChanged();

private:
    Person *m_host;
    QList<Person *> m_guests;
};

QML_DECLARE_TYPEINFO(BirthdayParty, QML_HAS_ATTACHED_PROPERTIES)

#endif // BIRTHDAYPARTY_H

birthdayparty.cpp:

#include "birthdayparty.h"

BirthdayPartyAttached::BirthdayPartyAttached(QObject *object)
: QObject(object)
{
}

QDate BirthdayPartyAttached::rsvp() const
{
    return m_rsvp;
}

void BirthdayPartyAttached::setRsvp(const QDate &d)
{
    if (d != m_rsvp) {
        m_rsvp = d;
        emit rsvpChanged();
    }
}

BirthdayParty::BirthdayParty(QObject *parent)
: QObject(parent), m_host(0)
{
}

Person *BirthdayParty::host() const
{
    return m_host;
}

void BirthdayParty::setHost(Person *c)
{
    if (c == m_host) return;
    m_host = c;
    emit hostChanged();
}

QQmlListProperty<Person> BirthdayParty::guests()
{
    return QQmlListProperty<Person>(this, m_guests);
}

int BirthdayParty::guestCount() const
{
    return m_guests.count();
}

Person *BirthdayParty::guest(int index) const
{
    return m_guests.at(index);
}

void BirthdayParty::startParty()
{
    QTime time = QTime::currentTime();
    emit partyStarted(time);
}

QString BirthdayParty::announcement() const
{
    return QString();
}

void BirthdayParty::setAnnouncement(const QString &speak)
{
    qWarning() << qPrintable(speak);
}

BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object)
{
    return new BirthdayPartyAttached(object);
}

在该系列第9个例子中,我们接触到了HappyBirthdaySong类,它是一个自定义的Property Value Source,用来为QML属性提供随时间变化的能力,类似于Animation。在该例子中,它被用于announcement属性。

happybirthdaysong.h:

#ifndef HAPPYBIRTHDAYSONG_H
#define HAPPYBIRTHDAYSONG_H

#include <QQmlPropertyValueSource>
#include <QQmlProperty>

#include <QStringList>

class HappyBirthdaySong : public QObject, public QQmlPropertyValueSource
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_INTERFACES(QQmlPropertyValueSource)
public:
    HappyBirthdaySong(QObject *parent = 0);

    virtual void setTarget(const QQmlProperty &);

    QString name() const;
    void setName(const QString &);

private slots:
    void advance();

signals:
    void nameChanged();
private:
    int m_line;
    QStringList m_lyrics;
    QQmlProperty m_target;
    QString m_name;
};

#endif // HAPPYBIRTHDAYSONG_H

happybirthdaysong.cpp:

#include "happybirthdaysong.h"
#include <QTimer>

HappyBirthdaySong::HappyBirthdaySong(QObject *parent)
: QObject(parent), m_line(-1)
{
    setName(QString());
    QTimer *timer = new QTimer(this);
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance()));
    timer->start(1000);
}

void HappyBirthdaySong::setTarget(const QQmlProperty &p)
{
    m_target = p;
}

QString HappyBirthdaySong::name() const
{
    return m_name;
}

void HappyBirthdaySong::setName(const QString &name)
{
    if (m_name == name)
        return;

    m_name = name;

    m_lyrics.clear();
    m_lyrics << "Happy birthday to you,";
    m_lyrics << "Happy birthday to you,";
    m_lyrics << "Happy birthday dear " + m_name + ",";
    m_lyrics << "Happy birthday to you!";
    m_lyrics << "";

    emit nameChanged();
}

void HappyBirthdaySong::advance()
{
    m_line = (m_line + 1) % m_lyrics.count();

    m_target.write(m_lyrics.at(m_line));
}

在main.cpp中将这些C++类注册成QML类型后,我们就可以在QML中创建一个实例化的BirthdayParty,并对其属性赋值:

example.qml:

import People 1.0
import QtQuick 2.0  // For QColor

// ![0]
BirthdayParty {
    id: theParty

    HappyBirthdaySong on announcement { name: theParty.host.name }      // 属性绑定

    host: Boy {
        name: "Bob Jones"
        shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 }
    }
// ![0]
    onPartyStarted: console.log("This party started rockin' at " + time);

    Boy {
        name: "Leo Hodges"
        BirthdayParty.rsvp: "2009-07-06"
        shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 }
    }
    Boy {
        name: "Jack Smith"
        shoe { size: 8; color: "blue"; brand: "Puma"; price: 19.95 }
    }
    Girl {
        name: "Anne Brown"
        BirthdayParty.rsvp: "2009-07-01"
        shoe.size: 7
        shoe.color: "red"
        shoe.brand: "Marc Jacobs"
        shoe.price: 699.99
    }

// ![1]
}
// ![1]

最后,在main.cpp调用这个属性的信息,并基于一定的规则输出这些信息:

#include <QCoreApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include "birthdayparty.h"
#include "happybirthdaysong.h"
#include "person.h"

int main(int argc, char ** argv)
{
    QCoreApplication app(argc, argv);
    qmlRegisterType<BirthdayPartyAttached>();
    qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
    qmlRegisterType<HappyBirthdaySong>("People", 1,0, "HappyBirthdaySong");
    qmlRegisterType<ShoeDescription>();
    qmlRegisterType<Person>();
    qmlRegisterType<Boy>("People", 1,0, "Boy");
    qmlRegisterType<Girl>("People", 1,0, "Girl");

    QQmlEngine engine;
    QQmlComponent component(&engine, QUrl("qrc:example.qml"));
    BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create());

    if (party && party->host()) {
        qWarning() << party->host()->name() << "is having a birthday!";

        if (qobject_cast<Boy *>(party->host()))
            qWarning() << "He is inviting:";
        else
            qWarning() << "She is inviting:";

        for (int ii = 0; ii < party->guestCount(); ++ii) {
            Person *guest = party->guest(ii);

            QDate rsvpDate;
            QObject *attached =
                qmlAttachedPropertiesObject<BirthdayParty>(guest, false);
            if (attached)
                rsvpDate = attached->property("rsvp").toDate();

            if (rsvpDate.isNull())
                qWarning() << "   " << guest->name() << "RSVP date: Hasn't RSVP'd";
            else
                qWarning() << "   " << guest->name() << "RSVP date:" << qPrintable(rsvpDate.toString());
        }

        party->startParty();
    } else {
        qWarning() << component.errors();
    }

    return app.exec();
}

输出如下:

Qt5官方demo解析集30——Extending QML - Binding Example

时间: 2024-08-04 16:02:44

Qt5官方demo解析集30——Extending QML - Binding Example的相关文章

Qt5官方demo解析集29——Extending QML - Property Value Source Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集28--Extending QML - Signal Support Example 我们经常会在QML代码中使用Animation和bindings,以使得我们的程序具有更好的动态性能.那么,类似NumberAnimation这种QML类似实际上是提供了一个算法来为属性提供动态变化的数值,或者说是提供了一个值的集合.这里

Qt5官方demo解析集28——Extending QML - Signal Support Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集27--Extending QML - Attached Properties Example 这个demo演示了为QML自定义类型添加信号的方法,这与Qt5官方demo解析集16--Chapter 2: Connecting to C++ Methods and Signals所介绍的差不多,鉴于例子的尺寸,可能那一篇要

Qt5官方demo解析集21——Extending QML - Adding Types Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 又是一个新的系列了,不过这个系列和我们之前的Chapter系列是及其相似的,但是不过呢,Chapter主要演示了如何使用C++创建具有可视性的类型以扩展我们的QML,而这个系列则关注于如何使用C++扩展QML非可视化的内容. 这里第一个小例子与Chapter的第一个小例子及其类似: person是我们自定义的C++类,然后我们将其注册为QML类型供资源

Qt5官方demo解析集26——Extending QML - Grouped Properties Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集25--Extending QML - Methods Example 如果之前看过了我前面介绍粒子系统的朋友,应该对 velocity: AngleDirection {angleVariation: 360; magnitude: 80; magnitudeVariation: 40} 这样的属性设置格式屡见不鲜了,它

Qt5官方demo解析集24——Extending QML - Default Property Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集23--Extending QML - Inheritance and Coercion Example 有时我们看到某个QML类型的声明中,某些数据并没有放在属性 + :后面,它们实际上属于这个类型的默认属性.而这个demo则向我们介绍了这个技术. 这个例子与上一篇几乎没有什么改动,除了两个小地方. 第一处在QML描述文

Qt5官方demo解析集25——Extending QML - Methods Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集24--Extending QML - Default Property Example 这个例子主要向我们介绍了在QML类型中定义函数的方法. person.h: #ifndef PERSON_H #define PERSON_H #include <QObject> class Person : public QOb

Qt5官方demo解析集22——Extending QML - Object and List Property Types Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集21--Extending QML - Adding Types Example 在上一个例子中我们基于C++创建了一个自定义的QML类型,接下来,我们将该类作为另一个类的属性类型,定义了另一个birthdayparty类.这个例子与Qt5官方demo解析集19--Chapter 5: Using List Propert

Qt5官方demo解析集23——Extending QML - Inheritance and Coercion Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集22--Extending QML - Object and List Property Types Example 在上一个例子中,我们为BirthdayParty类创建了带有一个列表参数的属性guests,而这个列表参数的类型都是一致的,即Person.然而,使用QML的类型转换机制,我们可以使这个列表参数的类型变得不

Qt5官方demo解析集31——StocQt

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集30--Extending QML - Binding Example 最近在做QML制表,因此想找一些相关曲线绘制的demo看看,结果发现了这个例子,觉得挺不错,它相比于我们之前的Extend和Particle系列显得更大一些,涉及到的面也更广一些.因此想拿过来给大家分享~ 这个例子是基于QML的股票走势图绘制,数据来源