QT5中如何自定義窗口部件

提高法

eg.(定義一個新的QLable部件)
一、定義一個類
class Label : public base, public QLabel //能夠支持多重繼承
二、在qt creator中打開ui編輯器,拖曳一個QLable兌現,提高,輸入提高的類名Label,勾選所有包含,添加,提高成功。


插件法
Qt Assistance:Creating Custom Widgets for Qt Designer
一、和提高法同樣,也須要先創建一個新的部件類
class AnalogClock : public QWidget
二、創建一個插件類
class AnalogClockPlugin : public QObject, public QDesignerCustomWidgetInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface" FILE "analogclock.json")
    Q_INTERFACES(QDesignerCustomWidgetInterface)
public:
    explicit AnalogClockPlugin(QObject *parent = 0);
    //……
};
並在其cpp中實現其相關函數
三、修改.pro文件
……
CONFIG      += plugin
CONFIG      += designer
CONFIG      += debug_and_release
TEMPLATE    = lib
QT          += widgets designer
SOURCES += \
    analogclock.cpp \
    analogclockplugin.cpp
HEADERS  += \
    analogclock.h \
    analogclockplugin.h
OTHER_FILES += analogclock.json
target.path = $$[QT_INSTALL_PLUGINS]/designer

INSTALLS += targetjson

……編輯器

四、在項目文件目錄下建一個空的analogclock.json文件

五、編譯生成.dll文件,而後將其放置到C:\Qt\Qt5.2.0\5.2.0\msvc2012_64_opengl\plugins\designer下就能夠在qt designer中使用該自定義插件了。函數

注:若是須要在qt Creator中使用該插件,因爲Qt SDK for Windows的兩部分是由不一樣編譯環境編譯而成,QtCreator是由msvc編譯,Qt庫是由mingw編譯,因此還須要將其複製到C:\Qt\Qt5.2.0\Tools\QtCreator\bin\plugins\designer,還要解決方法有如下幾種:工具

1) 下載QT Creator的源碼而後在QT Creator中用MinGW編譯
2) 將插件在Visual Studio下編譯Build the plugin with Visual Studio
3) 編譯QT Creator源碼,但將build key checking關掉ui

以後就能夠在 QtCreator中打開項目的界面文件(*.ui),此時QtCreator容許你使用集成的QtDesigner來編輯這個ui文件,而後打開菜單項「工具->界面編輯器->About Qt Designer plugins...」便可查看哪些插件加載成功了,哪些未加載成功(在單獨運行的QtDesigner中,打開「幫助->關於插件」菜單也可查看插件加載成功與否)。插件

相關文章
相關標籤/搜索