<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                本系列所有文章可以在這里查看[http://blog.csdn.net/cloud_castle/article/category/2123873](http://blog.csdn.net/cloud_castle/article/category/2123873) 接上文[Qt5官方demo解析集28——Extending QML - Signal Support Example](http://blog.csdn.net/cloud_castle/article/details/37512407) 我們經常會在QML代碼中使用Animation和bindings,以使得我們的程序具有更好的動態性能。那么,類似NumberAnimation這種QML類似實際上是提供了一個算法來為屬性提供動態變化的數值,或者說是提供了一個值的集合。這里Qt將其稱作“屬性值來源”(Property Value Source),并為這些QML類型提供了一個通用的接口,即QQmlPropertyValueSource。通過繼承這個類,我們可以實現自定義的Property Value Source。 在前面的項目中添加一個類happybirthdaysong,用來自定義地控制BirthdayParty中announcement屬性的變化: ![](https://box.kancloud.cn/2016-01-18_569cbd08ccdd9.jpg) 這個demo向我們展示了這個自定義的過程,happybirthdaysong.h: ~~~ #ifndef HAPPYBIRTHDAYSONG_H #define HAPPYBIRTHDAYSONG_H #include <QQmlPropertyValueSource> #include <QQmlProperty> #include <qqml.h> #include <QStringList> // ![0] class HappyBirthdaySong : public QObject, public QQmlPropertyValueSource // 由于QQmlPropertyValueSource是一個接口類 { // 我們還需要繼承QObject Q_OBJECT Q_INTERFACES(QQmlPropertyValueSource) // 聲明接口 // ![0] Q_PROPERTY(QString name READ name WRITE setName) // name屬性用來設置生日歌的對象 // ![1] public: HappyBirthdaySong(QObject *parent = 0); virtual void setTarget(const QQmlProperty &); // 用來指明作用的屬性對象 // ![1] // <PropertyValueSource> on <property>時被調用 QString name() const; // 自定義屬性的讀寫函數 void setName(const QString &); private slots: void advance(); // 更新函數,每秒輸出一句歌詞 private: int m_line; QStringList m_lyrics; QQmlProperty m_target; QString m_name; // ![2] }; // ![2] #endif // HAPPYBIRTHDAYSONG_H ~~~ happybirthdaysong.cpp: ~~~ #include "happybirthdaysong.h" #include <QTimer> HappyBirthdaySong::HappyBirthdaySong(QObject *parent) : QObject(parent), m_line(-1) // 初始化m_line為-1 { // 使advance()第一次被調用時輸出第一句歌詞 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; // 該類型作用于某個屬性時,Qt會使用這里的函數 } QString HappyBirthdaySong::name() const { return m_name; } void HappyBirthdaySong::setName(const QString &name) // 初始化歌詞,并帶上“姓名”參數 { 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 << ""; } void HappyBirthdaySong::advance() // 循環顯示的好方式 { m_line = (m_line + 1) % m_lyrics.count(); m_target.write(m_lyrics.at(m_line)); } ~~~ Person類沒有變化,而BirthdayParty類則單純地添加了一個屬性announcement來使上面的Source能作用其上,它與其他屬性沒有不同,類型為QString,用來賦予不同的歌詞。 ~~~ Q_PROPERTY(QString announcement READ announcement WRITE setAnnouncement) ~~~ 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(); } ~~~ 最后,這個Source需要通過 on 這樣的語句來調用, 因此在QML文件中添加了HappyBirthdaySongonannouncement{name:"BobJones"},使得這個程序得以循環地為Bob Jones唱生日快樂歌: example.qml: ~~~ import People 1.0 import QtQuick 2.0 // For QColor // ![0] BirthdayParty { HappyBirthdaySong on announcement { name: "Bob Jones" } // ![0] onPartyStarted: console.log("This party started rockin' at " + time); host: Boy { name: "Bob Jones" shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } } 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] ~~~ 效果如下: ![](https://box.kancloud.cn/2016-01-18_569cbd08e3c11.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看