• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            Q_DECLARE_METATYPE與qRegisterMetaType學(xué)習(xí)(收集)

            Posted on 2011-04-15 22:34 RTY 閱讀(2866) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): 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
              • 如果要使自定義類(lèi)型或其他非QMetaType內(nèi)置類(lèi)型在QVaiant中使用,必須使用該宏。
              • 該類(lèi)型必須有公有的 構(gòu)造、析構(gòu)、復(fù)制構(gòu)造 函數(shù)
            • qRegisterMetaType 必須使用該函數(shù)的兩種情況
              • 如果非QMetaType內(nèi)置類(lèi)型要在 Qt 的屬性系統(tǒng)中使用
              • 如果非QMetaType內(nèi)置類(lèi)型要在 queued 信號(hào)與槽 中使用

            二者關(guān)系

            二者的代碼:

            • Q_DECLARE_METATYPE 展開(kāi)后是一個(gè)特化后的類(lèi) QMetaTypeId<TYPE>

            • qRegisterMetaType 將某類(lèi)型注冊(cè)中 MetaType 系統(tǒng)中

            二者的聯(lián)系:

            • QMetaTypeId<TYPE>的類(lèi)中的成員包含對(duì)qRegisterMetaType的調(diào)用

            • 我們知道類(lèi)中的成員函數(shù)并不一定會(huì)被調(diào)用(即,該宏并不確保類(lèi)型被注冊(cè)到MetaType)。

            • 通過(guò)qRegisterMetaType可以確保類(lèi)型被注冊(cè)

            兩個(gè)qRegisterMetaType 的聯(lián)系

            • 無(wú)參的qRegisterMetaType函數(shù)會(huì)通過(guò)該成員調(diào)用帶參數(shù)的qRegisterMetaType()

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

            Q_DECLARE_METATYPE

            代碼來(lái)源: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

            • 宏展開(kāi)是一個(gè)在Qt的命名空間中的一個(gè)類(lèi)模板的特化 QMetaTypeId<TYPE>

            • 該類(lèi)含一個(gè)enum和一個(gè)返回!QMetaType的id的成員函數(shù)

            qRegisterMetaType(const char *typeName)

            代碼來(lái)源: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));
            }
            • 該函數(shù)的核心就是調(diào)用了registerType 函數(shù)
            • 兩個(gè)Helper模板函數(shù)分別對(duì)構(gòu)造和析構(gòu)函數(shù)進(jìn)行封裝

            registerType

            代碼來(lái)源:src/corelib/kernel/qmetatype.cpp

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

            函數(shù)功能:

            • 根據(jù)類(lèi)型名查找其MetaType類(lèi)型,如果已存在,則直接返回;否則創(chuàng)建后返回。

            • 創(chuàng)建一個(gè) !QCustomTypeInfo 對(duì)象
            • 該對(duì)象包含要類(lèi)型的構(gòu)造、析構(gòu)信息,已經(jīng)規(guī)范化后的類(lèi)型名
            • 該對(duì)象存入一個(gè)全局的!QVector中

            qRegisterMetaType()

            看manual,可以知道,qRegisterMetaType 還有一個(gè)無(wú)參的重載函數(shù)。

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

            unregisterType(const char *typeName)

            函數(shù)的作用是取消自己先前注冊(cè)的某個(gè)metatype類(lèi)型。

            前面提到注冊(cè)信息在一個(gè)全局的 QVector<QCustomTypeInfo>中,當(dāng)取消注冊(cè)的時(shí)候是怎么樣的呢?直接刪除Vector中相應(yīng)的項(xiàng)么?源碼告訴我們,不是的。

            實(shí)際是查找到相應(yīng)的項(xiàng),清空該項(xiàng)的內(nèi)容。

            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;
             }
            }

             

              久久人人爽人人爽人人AV| 狠狠色丁香久久婷婷综合_中| 国产69精品久久久久99尤物| 久久久久亚洲精品中文字幕| 欧美亚洲另类久久综合| 国产一区二区精品久久| 丁香五月综合久久激情| 亚洲国产精品成人久久蜜臀| 狠狠色综合网站久久久久久久| 色综合久久久久久久久五月| 久久99精品国产麻豆婷婷| 国产精品成人99久久久久 | 亚洲国产精品无码久久青草| 久久久久久久久久久免费精品| 日本欧美国产精品第一页久久| 久久精品国产91久久综合麻豆自制| 国产成人无码精品久久久性色| 国产欧美久久久精品| 国产精品久久波多野结衣| 色婷婷综合久久久久中文字幕| AV色综合久久天堂AV色综合在 | 久久人人妻人人爽人人爽| 久久综合噜噜激激的五月天| 国产高潮国产高潮久久久91 | 国产精品久久精品| 久久综合九色综合网站| 日韩久久久久中文字幕人妻| 久久精品一区二区国产| 久久99九九国产免费看小说| 久久国产高清一区二区三区| 蜜桃麻豆www久久| 久久午夜无码鲁丝片| 久久综合偷偷噜噜噜色| 久久伊人影视| 国产精品女同一区二区久久| 久久国产免费直播| 欧美久久久久久精选9999| 欧美精品一区二区久久| 97精品国产91久久久久久| 久久青青草原精品影院| 国产精久久一区二区三区|