• <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)系 :: 聚合  :: 管理

            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內(nèi)置類型在QVaiant中使用,必須使用該宏。
              • 該類型必須有公有的 構(gòu)造、析構(gòu)、復(fù)制構(gòu)造 函數(shù)
            • qRegisterMetaType 必須使用該函數(shù)的兩種情況
              • 如果非QMetaType內(nèi)置類型要在 Qt 的屬性系統(tǒng)中使用
              • 如果非QMetaType內(nèi)置類型要在 queued 信號(hào)與槽 中使用

            二者關(guān)系

            二者的代碼:

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

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

            二者的聯(lián)系:

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

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

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

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

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

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

            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

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

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

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

            registerType

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

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

            函數(shù)功能:

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

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

            qRegisterMetaType()

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

            template <typename T>
            inline int qRegisterMetaType()
            {
            return qMetaTypeId(static_cast<T *>(0));
            }
            • 函數(shù)看起來和帶參數(shù)的那個(gè)似乎區(qū)別很大(難道不是么?)。
            • 手冊(cè)中告訴我們,執(zhí)行這個(gè)的時(shí)候,模板參數(shù)T必須用 Q_DECLARE_METATYPE() 聲明過
            • 能猜到原因嗎?注意看前面 Q_DECLARE_METATYPE() 代碼,
            • 對(duì)了。類中的成員函數(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類型。

            前面提到注冊(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麻豆网站 | 久久人与动人物a级毛片| 亚洲va久久久噜噜噜久久狠狠 | 国产亚洲色婷婷久久99精品| 久久久久久狠狠丁香| 热久久国产欧美一区二区精品| 亚洲欧洲日产国码无码久久99| 曰曰摸天天摸人人看久久久| 无码人妻久久一区二区三区蜜桃 | 亚洲AV伊人久久青青草原| 久久婷婷五月综合色高清| 久久精品无码一区二区app| 久久久久亚洲av无码专区导航| 青青久久精品国产免费看| 69久久夜色精品国产69 | 日本久久中文字幕| 国产精品一区二区久久| 久久免费看黄a级毛片| 国产毛片久久久久久国产毛片| 精品久久久久久无码专区不卡| 综合久久精品色| 久久久久久国产精品免费免费 | 久久高清一级毛片| 人妻精品久久无码专区精东影业 | 无码人妻少妇久久中文字幕| 久久久青草久久久青草| 国产精品99精品久久免费| 亚洲国产一成人久久精品| 久久精品成人免费国产片小草| 久久w5ww成w人免费| 色诱久久久久综合网ywww | 色综合合久久天天综合绕视看 | 久久免费香蕉视频| 国产99久久九九精品无码| 88久久精品无码一区二区毛片| 久久精品国产亚洲av影院| 欧美熟妇另类久久久久久不卡| 久久精品国产AV一区二区三区| 狠狠色婷婷久久综合频道日韩| 久久人妻少妇嫩草AV蜜桃|