<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 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## 元對象實現工廠 **場景** 頭文件都沒有只知道類的名字,在這種情況下需要將對象實例化出來,同時還要調用類中的方法。 <details> <summary> person.h</summary> ``` #ifndef PERSON_H #define PERSON_H #include <QObject> #include <QDebug> class Person : public QObject { Q_OBJECT public: Q_INVOKABLE explicit Person(QObject *parent = nullptr); // 元對象調用的函數必須卸載 slots 中 public slots: void show(){ qDebug()<<__FUNCTION__<<metaObject()->className(); } QString echo(QString hello){ return QString("word %1").arg(hello); } }; #endif // PERSON_H ``` </details> <br /> <details> <summary> student.h</summary> ``` #ifndef STUDENT_H #define STUDENT_H #include "person.h" class Student : public Person { Q_OBJECT public: Q_INVOKABLE explicit Student(QObject* parent=nullptr); }; #endif // STUDENT_H ``` </details> <br /> <details> <summary> createperson.h</summary> ``` #ifndef CREATEPERSON_H #define CREATEPERSON_H #include <QHash> #include <QObject> #include <QString> #include "student.h" class CreatePerson { public: CreatePerson(); QObject* newPerson(QString name); private: QHash<QString,const QMetaObject*> m_members; }; #endif // CREATEPERSON_H ``` </details> <br /> <details> <summary> createperson.cpp</summary> ``` #include "createperson.h" #include <QDebug> CreatePerson::CreatePerson() { m_members["student"]=&Student::staticMetaObject; //add m_members["teacher"] ... } QObject *CreatePerson::newPerson(QString name) { if(!m_members.contains(name)){ qDebug()<<"your class name is not found" <<name; return nullptr; } return m_members[name]->newInstance(); } ``` </details> <br /> 調用 ``` CreatePerson person; QObject *student = person.newPerson("student"); QMetaObject::invokeMethod(student,"show"); // 調用有參數和返回值的 QString result; QMetaObject::invokeMethod(student,"echo",Qt::AutoConnection, Q_RETURN_ARG(QString,result), Q_ARG(QString,"hello")); qDebug()<<result; ``` > 需要是QObject子類,同時需要聲明Q\_OBJECT,這樣才能享受到元對象系統帶來的福利。 > 元對象調用的函數必須在 slots 中聲明
                  <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>

                              哎呀哎呀视频在线观看