<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解析集21——Extending QML - Adding Types Example](http://blog.csdn.net/cloud_castle/article/details/37055429) 在上一個例子中我們基于C++創建了一個自定義的QML類型,接下來,我們將該類作為另一個類的屬性類型,定義了另一個birthdayparty類。這個例子與[Qt5官方demo解析集19——Chapter 5: Using List Property Types](http://blog.csdn.net/cloud_castle/article/details/36898495)是十分相似的,只不過在這個例子中只提供了對列表屬性的訪問能力。[](http://blog.csdn.net/cloud_castle/article/details/36898495) 項目文件如下: ![](https://box.kancloud.cn/2016-01-18_569cbd083f9a1.jpg) Person的定義和聲明與上一篇博文中所貼出的一致,因此我們先看看birthdayparty.h: ~~~ #ifndef BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H #include <QObject> #include <QQmlListProperty> // 這個類型我們在解析集19中有討論過,它用來提供帶有列表成員的屬性 #include "person.h" // ![0] class BirthdayParty : public QObject { Q_OBJECT // ![0] // ![1] Q_PROPERTY(Person *host READ host WRITE setHost) // host屬性,指向一個Person對象 // ![1] // ![2] Q_PROPERTY(QQmlListProperty<Person> guests READ guests) // guests屬性,指向一個Person列表 // ![2] // ![3] public: BirthdayParty(QObject *parent = 0); Person *host() const; void setHost(Person *); QQmlListProperty<Person> guests(); // 上面是屬性的讀寫函數 int guestCount() const; // 下面是自定義的用來返回客人數與讀取某個客人的函數 Person *guest(int) const; private: Person *m_host; QList<Person *> m_guests; // 對應的需要將m_guests定義成QList<Person *>類型的成員列表 }; // ![3] #endif // BIRTHDAYPARTY_H ~~~ birthdayparty.cpp: ~~~ #include "birthdayparty.h" BirthdayParty::BirthdayParty(QObject *parent) : QObject(parent), m_host(0) // 初始化參數賦值,這個0相當于NULL { } // ![0] Person *BirthdayParty::host() const { return m_host; } void BirthdayParty::setHost(Person *c) { m_host = c; } QQmlListProperty<Person> BirthdayParty::guests() { return QQmlListProperty<Person>(this, m_guests); // 由一個QList得到QQmlListProperty值,函數原型如下 } int BirthdayParty::guestCount() const // 數據成員一般都為私有,要調用它的函數,我們需要提供一個公共的接口 { return m_guests.count(); } Person *BirthdayParty::guest(int index) const { return m_guests.at(index); } ~~~ ![](https://box.kancloud.cn/2016-01-18_569cbd084e8e6.jpg) example.qml: ~~~ import People 1.0 // 由于在main函數中將Person與Birthdayparty的名稱空間均注冊為People 1.0,導入它我們可以同時使用這兩個類型 // ![0] BirthdayParty { host: Person { // host參數為單Person name: "Bob Jones" shoeSize: 12 } guests: [ // guests參數為多Person Person { name: "Leo Hodges" }, Person { name: "Jack Smith" }, Person { name: "Anne Brown" } ] } // ![0] ~~~ main.cpp: ~~~ #include <QCoreApplication> #include <QQmlEngine> #include <QQmlComponent> #include <QDebug> #include "birthdayparty.h" #include "person.h" int main(int argc, char ** argv) { QCoreApplication app(argc, argv); //![register list] qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty"); // QML類型注冊 qmlRegisterType<Person>("People", 1,0, "Person"); //![register list] QQmlEngine engine; QQmlComponent component(&engine, QUrl("qrc:example.qml")); BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create()); if (party && party->host()) { // 由于host為"Bob Jones",該if得以進入 qWarning() << party->host()->name() << "is having a birthday!"; qWarning() << "They are inviting:"; for (int ii = 0; ii < party->guestCount(); ++ii) // 調用公共接口得到客人數 qWarning() << " " << party->guest(ii)->name(); // 逐一訪問其姓名 } else { qWarning() << component.errors(); } return 0; } ~~~
                  <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>

                              哎呀哎呀视频在线观看