• <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>

            codeArt

            codeArt

            QT中的插件

             

            QT有著獨(dú)特的插件管理方法便于使用,調(diào)理清晰.完全可以替代WIN32下的動(dòng)態(tài)庫,靜態(tài)庫.不過,QT也支持動(dòng)態(tài)庫和靜態(tài)庫加載 .QLibrary,最終,QLibrary調(diào)用WIN32下的LoadLibrary,GetProcAddress函數(shù).


            Qt插件的使用方法:

            [1]project_main_1工程中定義接口

            class interface__1
            {
            public:
                
            void __func1() = 0;
                
            void __func2() = 0;
                
            void __func3() = 0;
            }
            ;
                
            class interface__2
            {
            public:
                
            void __func4() = 0;
                
            void __func5() = 0;
                
            void __func6() = 0;
            }
            ;

            [2]project_plugin_1工程中實(shí)現(xiàn)接口

            class derive__1:public interface__1,interface__2
            {
            public:
                
            void __func1();
                
            void __func2();
                
            void __func3();
                
            void __func4();
                
            void __func5();
                
            void __func6();
            }
            ;

            [3]project_main_1中使用QPluginLoader,QPluginLoader內(nèi)部實(shí)現(xiàn)也是使用LoadLibrary,GetProcAddress,稍后會(huì)有說明

            用法1:

            QobjectList objList = QpluginLoader::staticInstances();
            for(int i = 0; i<objList.size(); i++)
            {
                interface__1 
            *inter1 = qobject_cast< interface__1 *>(objList[i]);
                interface__2 
            *inter1 = qobject_cast< interface__2 *>(objList[i]);    
            }

            用法2:

            QpluginLoader pl(“plugin path”);
            Qobject
            * plugin = pl.instance();

            這里可以看出,充分的使用了對象對象的多態(tài).那么,QpluginLoader是如何實(shí)現(xiàn)的呢?

            看下面細(xì)節(jié).

            Qt的類幾乎所有的都有一個(gè)QT_class+private的類,用來實(shí)現(xiàn)具體邏輯,暴露給我們的類定義通用的接口.QpluginLoader的內(nèi)部類是QLibraryPrivate,QLibrary是同一個(gè).

            [1]如何加載

            bool QLibraryPrivate::loadPlugin()
            {
                
            if (instance) {
                    libraryUnloadCount.
            ref();
                    
            return true;
                }

                
            if (load()) {//這里最終調(diào)用load_sys()
                    instance =    (QtPluginInstanceFunction)resolve("qt_plugin_instance");//注意這里的 qt_plugin_instance,插件里面必然導(dǎo)出該函數(shù)名稱
                    return instance;
                }

                
            return false;
            }


            bool QLibraryPrivate::load_sys()
            {
            #ifdef Q_OS_WINCE
                QString attempt 
            = QFileInfo(fileName).absoluteFilePath();
            #else
                QString attempt 
            = fileName;
            #endif

                
            //avoid 'Bad Image' message box
                UINT oldmode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
                pHnd 
            = LoadLibrary((wchar_t*)QDir::toNativeSeparators(attempt).utf16());

                
            if (pluginState != IsAPlugin) {
                    
            if (!pHnd && ::GetLastError() == ERROR_MOD_NOT_FOUND) {
                        attempt 
            += QLatin1String(".dll");
                        pHnd 
            = LoadLibrary((wchar_t*)QDir::toNativeSeparators(attempt).utf16());
                    }

                }


                SetErrorMode(oldmode);
                
            if (!pHnd) {
                    errorString 
            = QLibrary::tr("Cannot load library %1: %2").arg(fileName).arg(qt_error_string());
                }

                
            if (pHnd) {
                    errorString.clear();

                    wchar_t buffer[MAX_PATH];
                    ::GetModuleFileName(pHnd, buffer, MAX_PATH);
                    attempt 
            = QString::fromWCharArray(buffer);

                    
            const QDir dir =  QFileInfo(fileName).dir();
                    
            const QString realfilename = attempt.mid(attempt.lastIndexOf(QLatin1Char('\\')) + 1);
                    
            if (dir.path() == QLatin1String("."))
                        qualifiedFileName 
            = realfilename;
                    
            else
                        qualifiedFileName 
            = dir.filePath(realfilename);
                }

                
            return (pHnd != 0);
            }

            [2] qt_plugin_instance是定義導(dǎo)出的呢?

            在實(shí)現(xiàn)接口時(shí),必須加上Q_EXPORT_PLUGIN2,Q_EXPORT_PLUGIN2 ( PluginName, ClassName )

            宏定義:

            #  define Q_EXPORT_PLUGIN2(PLUGIN, PLUGINCLASS)      \
                        Q_PLUGIN_VERIFICATION_DATA \
                        Q_EXTERN_C Q_DECL_EXPORT \
                        
            const char * Q_STANDARD_CALL qt_plugin_query_verification_data() \
                        
            return qt_plugin_verification_data; } \
                        Q_EXTERN_C Q_DECL_EXPORT QT_PREPEND_NAMESPACE(QObject) 
            * Q_STANDARD_CALL qt_plugin_instance() \
                        Q_PLUGIN_INSTANCE(PLUGINCLASS)
            其中
            #  define Q_PLUGIN_VERIFICATION_DATA \
                
            static const char *qt_plugin_verification_data = \
                  
            "pattern=""QT_PLUGIN_VERIFICATION_DATA""\n" \
                  
            "version="QT_VERSION_STR"\n" \
                  
            "debug="QPLUGIN_DEBUG_STR"\n" \
                  
            "buildkey="QT_BUILD_KEY;
            #define  Q_EXTERN_C extern
            #define  Q_DECL_EXPORT __declspec(dllexport)
            #define Q_PLUGIN_INSTANCE(IMPLEMENTATION) \
                    
            { \
                        
            static QT_PREPEND_NAMESPACE(QPointer)<QT_PREPEND_NAMESPACE(QObject)> _instance; \
                        
            if (!_instance)      \
                            _instance 
            = new IMPLEMENTATION; \
                        
            return _instance; \
                    }
            去掉宏之后,是2個(gè)函數(shù).
            static const char *qt_plugin_verification_data =           "pattern=""QT_PLUGIN_VERIFICATION_DATA""\n"     "version="QT_VERSION_STR"\n" 
                
            "debug="QPLUGIN_DEBUG_STR"\n"
                      
            "buildkey="QT_BUILD_KEY;
            extern __declspec(dllexport) qt_plugin_query_verification_data()
            {
                
            return  qt_plugin_verification_data;
            }


            extern __declspec(dllexport) QObject* qt_plugin_instance()
            {
                Qpoint
            <QOjbect> _instance;
                
            if (!_instance)
                            _instance 
            = new PLUGINCLASS;
                        
            return _instance;
            }

            [3] instancetypedef QObject *(*QtPluginInstanceFunction)();


            這樣就實(shí)現(xiàn)了QT的插件.但是還沒完.

            在定義接口時(shí),還應(yīng)加上Q_DECLARE_INTERFACE,This macro associates the given Identifier (a string literal) to the interface class called ClassName. The Identifier must be unique.

            #  define Q_DECLARE_INTERFACE(IFace, IId) \
                template 
            <> inline const char *qobject_interface_iid<IFace *>() \
                
            return IId; } \
                template 
            <> inline IFace *qobject_cast<IFace *>(QObject *object) \
                
            return reinterpret_cast<IFace *>((object ? object->qt_metacast(IId) : 0)); } \
                template 
            <> inline IFace *qobject_cast<IFace *>(const QObject *object) \
                
            return reinterpret_cast<IFace *>((object ? const_cast<QObject *>(object)->qt_metacast(IId) : 0)); }
            #endif // Q_MOC_RUN

            posted on 2010-08-28 19:56 codeArt 閱讀(5727) 評(píng)論(0)  編輯 收藏 引用 所屬分類: QT


            只有注冊用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章檔案

            編程與開源

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            国内精品伊人久久久久| 99久久99久久精品国产片果冻| 一本色道久久综合| 久久九九精品99国产精品| 精品熟女少妇aⅴ免费久久| 中文字幕人妻色偷偷久久| 青青草国产成人久久91网| 99精品国产99久久久久久97| 2022年国产精品久久久久| 狠狠色丁香婷婷久久综合 | 久久精品午夜一区二区福利| 99久久婷婷国产一区二区| 狠狠色丁香久久婷婷综合| 国产日韩久久久精品影院首页| 久久天天躁狠狠躁夜夜96流白浆| 欧美亚洲国产精品久久久久| 久久99毛片免费观看不卡| 久久99久国产麻精品66| 亚洲午夜无码久久久久| 久久久久久青草大香综合精品| 久久亚洲av无码精品浪潮| 久久强奷乱码老熟女| 人妻无码αv中文字幕久久琪琪布| 久久久久久国产精品免费免费| 国产精品久久久久jk制服| 男女久久久国产一区二区三区| 久久免费大片| 久久五月精品中文字幕| 超级碰久久免费公开视频| 97久久超碰国产精品2021| 久久久无码人妻精品无码| 亚洲中文字幕无码久久综合网| 国内精品久久久久影院老司| 久久久WWW成人| 欧美亚洲国产精品久久久久| 婷婷国产天堂久久综合五月| 亚洲国产成人精品91久久久 | 免费精品久久久久久中文字幕| 亚洲女久久久噜噜噜熟女| 亚洲国产一成人久久精品| 国产aⅴ激情无码久久|