加載插件是整個屬性設計器的第一步要打通的功能,插件中的控件都加載不了,後面就別搞別玩下去了無法玩的,要從一個動態庫中加載出來控件,確定須要用到反射機制,之前作.NET開發的時候就以爲反射這個東西至關強大,竟然能夠讀取DLL加載出來控件,如今用Qt,發現Qt也有反射機制,也許這東東可能各大開發語言平臺都具有吧,Qt反射對應的類叫QMetaObject,着實強大,其實整個Qt開發框架也是超級強大的,本人自從轉爲Qt開發爲主後,就深深的愛上了她,在其餘跨平臺的GUI開發框架平臺面前,都會被Qt秒成渣,Qt的跨平臺性是毋庸置疑的,幾十兆的內存存儲空間便可運行,尤爲是嵌入式linux這種資源至關緊張的狀況下,Qt的性能發揮到極致。linux
體驗地址:https://gitee.com/feiyangqingyun/QUCSDK
https://github.com/feiyangqingyun/qucsdkc++
void frmMain::initPlugin() { //載入默認的插件 #if defined(Q_OS_WIN) QString pluginName = QString("%1/quc.dll").arg(qApp->applicationDirPath()); #elif defined(Q_OS_UNIX) QString pluginName = QString("%1/libquc.so").arg(qApp->applicationDirPath()); #elif defined(Q_OS_MAC) QString pluginName = QString("%1/libquc.dylib").arg(qApp->applicationDirPath()); #endif loadPlugin(pluginName); //載入默認的控件xml數據 openFile(qApp->applicationDirPath() + "/quc.xml"); qApp->processEvents(); ui->listWidget->verticalScrollBar()->setValue(0); } void frmMain::loadPlugin(const QString &fileName) { openPlugin(fileName); this->setWindowTitle(QString("自定義控件屬性設計器(共 %1 個控件)(QQ:517216493)").arg(ui->listWidget->count())); } void frmMain::openPlugin(const QString &fileName) { qDeleteAll(listWidgets); listWidgets.clear(); listNames.clear(); ui->listWidget->clear(); //加載自定義控件插件集合信息,包括得到類名+圖標 QPluginLoader loader(fileName); if (loader.load()) { QObject *plugin = loader.instance(); //獲取插件容器,而後逐個遍歷容器找出單個插件 QDesignerCustomWidgetCollectionInterface *interfaces = qobject_cast<QDesignerCustomWidgetCollectionInterface *>(plugin); if (interfaces) { listWidgets = interfaces->customWidgets(); int count = listWidgets.count(); for (int i = 0; i < count; i++) { QIcon icon = listWidgets.at(i)->icon(); QString className = listWidgets.at(i)->name(); QListWidgetItem *item = new QListWidgetItem(ui->listWidget); item->setText(className); item->setIcon(icon); listNames << className; } } //獲取全部插件的類名 const QObjectList objList = plugin->children(); foreach (QObject *obj, objList) { QString className = obj->metaObject()->className(); //qDebug() << className; } } }