• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 319, comments - 22, trackbacks - 0, articles - 11
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            Q_DECLARE_METATYPE與qRegisterMetaType學習(收集)

            Posted on 2011-04-15 22:34 RTY 閱讀(2880) 評論(0)  編輯 收藏 引用 所屬分類: Qt

            Registering custom types

            February 15th, 2009

            Just a note here, if you would have to pass custom data types between threads in Qt. As we know, a signal-slot connection is then (by default) of type Qt::QueuedConnection. Because in such a situation Qt needs to store passed parameters for a while, it creates their temporary copies. If it doesn’t recognize the passed data type, throws out an error:

             

            QObject::connect: Cannot queue arguments of type 'MyType'
                        

             

            So custom data types have to be registered using qRegisterMetaType(), like in the example:

             

            qRegisterMetaType<MyType>( "MyType" );
                        

             

            And this example is literal ⇒ when your class is called MyType, you register it as "MyType". Lastly I did something similar to this:

             

              1
                          2
                          3
                        
            typedef QMap<QString,QImage> MapStringImage;
                        (...)
                        qRegisterMetaType<MapStringImage>( "images" );
                        

             

            I didn’t get the error from QObject::connect (!), but also didn’t get things working. Wasted few hours hacking QMetaType class with no effect, and then more by accident than design changed "images" to "MapStringImage" and woo-hoo! That was my only problem… That’s why I’m stressing this naming issue, especially that documentation doesn’t tell a lot about it.

            BTW I needed to use typedef because otherwise Qt didn’t have a clue what to do with such a complex type.




            原文:http://hi.baidu.com/cyclone/blog/item/01108bd40599b00fa18bb793.html


            基本理解

            • Q_DECLARE_METATYPE
              • 如果要使自定義類型或其他非QMetaType內置類型在QVaiant中使用,必須使用該宏。
              • 該類型必須有公有的 構造、析構、復制構造 函數
            • qRegisterMetaType 必須使用該函數的兩種情況
              • 如果非QMetaType內置類型要在 Qt 的屬性系統中使用
              • 如果非QMetaType內置類型要在 queued 信號與槽 中使用

            二者關系

            二者的代碼:

            • Q_DECLARE_METATYPE 展開后是一個特化后的類 QMetaTypeId<TYPE>

            • qRegisterMetaType 將某類型注冊中 MetaType 系統中

            二者的聯系:

            • QMetaTypeId<TYPE>的類中的成員包含對qRegisterMetaType的調用

            • 我們知道類中的成員函數并不一定會被調用(即,該宏并不確保類型被注冊到MetaType)。

            • 通過qRegisterMetaType可以確保類型被注冊

            兩個qRegisterMetaType 的聯系

            • 無參的qRegisterMetaType函數會通過該成員調用帶參數的qRegisterMetaType()

            這兩個東西真難理清,不妨看看源碼吧。

            Q_DECLARE_METATYPE

            代碼來源:src/corelib/kernel/qmetatype.h

            #define Q_DECLARE_METATYPE(TYPE)                                        \
            QT_BEGIN_NAMESPACE \
            template <> \
            struct QMetaTypeId< TYPE > \
            { \
            enum { Defined = 1 }; \
            static int qt_metatype_id() \
            { \
            static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \
            if (!metatype_id) \
            metatype_id = qRegisterMetaType< TYPE >(#TYPE); \
            return metatype_id; \
            } \
            }; \
            QT_END_NAMESPACE

            • 宏展開是一個在Qt的命名空間中的一個類模板的特化 QMetaTypeId<TYPE>

            • 該類含一個enum和一個返回!QMetaType的id的成員函數

            qRegisterMetaType(const char *typeName)

            代碼來源:src/corelib/kernel/qmetatype.h

            template <typename T>
            int qRegisterMetaType(const char *typeName)
            {
            typedef void*(*ConstructPtr)(const T*);
            ConstructPtr cptr = qMetaTypeConstructHelper<T>;
            typedef void(*DeletePtr)(T*);
            DeletePtr dptr = qMetaTypeDeleteHelper<T>;

            return QMetaType::registerType(typeName, reinterpret_cast<QMetaType::Destructor>(dptr),
            reinterpret_cast<QMetaType::Constructor>(cptr));
            }
            • 該函數的核心就是調用了registerType 函數
            • 兩個Helper模板函數分別對構造和析構函數進行封裝

            registerType

            代碼來源:src/corelib/kernel/qmetatype.cpp

            int QMetaType::registerType(const char *typeName, Destructor destructor, Constructor constructor)

            函數功能:

            • 根據類型名查找其MetaType類型,如果已存在,則直接返回;否則創建后返回。

            • 創建一個 !QCustomTypeInfo 對象
            • 該對象包含要類型的構造、析構信息,已經規范化后的類型名
            • 該對象存入一個全局的!QVector中

            qRegisterMetaType()

            看manual,可以知道,qRegisterMetaType 還有一個無參的重載函數。

            template <typename T>
            inline int qRegisterMetaType()
            {
            return qMetaTypeId(static_cast<T *>(0));
            }
            • 函數看起來和帶參數的那個似乎區別很大(難道不是么?)。
            • 手冊中告訴我們,執行這個的時候,模板參數T必須用 Q_DECLARE_METATYPE() 聲明過
            • 能猜到原因嗎?注意看前面 Q_DECLARE_METATYPE() 代碼,
            • 對了。類中的成員函數qt_metatype_id中包含對qRegisterMetaType(typeName)的調用
            • 這兒就是輾轉調用了這個帶參數的qRegisterMetaType函數 

            unregisterType(const char *typeName)

            函數的作用是取消自己先前注冊的某個metatype類型。

            前面提到注冊信息在一個全局的 QVector<QCustomTypeInfo>中,當取消注冊的時候是怎么樣的呢?直接刪除Vector中相應的項么?源碼告訴我們,不是的。

            實際是查找到相應的項,清空該項的內容。

            for (int v = 0; v < ct->count(); ++v)
             {

             if (ct->at(v).typeName == typeName)
             {

             QCustomTypeInfo &inf = (*ct)[v];
             inf.typeName.clear();
             inf.constr = 0;
             inf.destr = 0;
             inf.alias = -1;
             }
            }

             

              国产精品亚洲综合专区片高清久久久| 久久久高清免费视频| 久久久精品人妻一区二区三区蜜桃| 合区精品久久久中文字幕一区| 亚洲欧洲日产国码无码久久99| 久久亚洲日韩精品一区二区三区| 99久久无码一区人妻| 久久SE精品一区二区| 国产精品欧美久久久久无广告| 日韩久久久久中文字幕人妻 | 久久夜色精品国产噜噜亚洲AV| 久久国产精品无码HDAV| 日韩十八禁一区二区久久| 国产精品一久久香蕉国产线看| 国产精品欧美久久久久天天影视| 超级97碰碰碰碰久久久久最新| 丰满少妇高潮惨叫久久久| 亚洲精品无码久久毛片| 久久九九有精品国产23百花影院| 久久婷婷五月综合国产尤物app | 日本加勒比久久精品| 久久99国产精品二区不卡| 久久婷婷国产综合精品| 国产精品亚洲综合久久| 久久久久久国产精品无码下载 | 久久精品国产精品亚洲精品 | yy6080久久| 噜噜噜色噜噜噜久久| 色综合久久88色综合天天 | 久久国产乱子伦精品免费午夜| 久久精品国产亚洲麻豆| 国产精品久久久久AV福利动漫| 久久久久亚洲AV成人片| 久久国产精品成人影院| 国产精品久久午夜夜伦鲁鲁| 久久精品国产亚洲AV无码偷窥| 久久精品天天中文字幕人妻| 亚洲伊人久久大香线蕉综合图片| 久久精品桃花综合| 久久久久人妻一区二区三区vr| 久久99久国产麻精品66|