Qt's custom controls or plug-ins

       When designing a form with Qt Designer interface, we can use the Widget Box in the Form Controls draw very convenient interface, such as a button dragged into a text editor and so on. Although the controls in Qt Designer to meet most of our needs, but sometimes also have some special needs, such as an input box, we have to enter the latitude and longitude, this time there will be two kinds of input methods for a is in decimal form, one is in the form of minutes and seconds, only this time using a simple LineEdit is unable to meet demand. We envisage constructing such a control input, it can support floating point input, but it also has a property, change this property so that it can switch the input in the form of latitude and longitude. If you need to enter the latitude and longitude on our multiple forms, then construct such a control that will be very convenient. Here as an example, explain how to create a custom form controls.

Step 1: Create a custom control project QtDesigner

Open Qt Creator, Qt Designer to create a custom control, as shown below:
Here Insert Picture Description
the wizard prompts to create a new class, named "Test" (Note: the first letter capitalized, not all lowercase, or later to be wrong), has been click "next point" to complete the project creation
Here Insert Picture Description
has been created works, as shown below:
Here Insert Picture Description

Step 2: Edit Project Code

In the new project, Test class does not ui interface file, we have to manually add up, if not ui interface file, the project can also be compiled through, but the control is added to ctreator after an open desinger tool when it will hang directly . So, we can put the original Test class test.c and test.h deleted, re-add a Test class.
Here Insert Picture Description
Create a Test class, as follows:
Here Insert Picture Description
in order to reduce the error probability, the interface does not do anything at this time.
Add "#include <QtUiPlugin / QDesignerExportWidget>" in the project header file "test.h", add "QDESIGNER_WIDGET_EXPORT" macro before the class name. As shown below:
Here Insert Picture Description

The third step: build the project

保存并选择release方式编译。先qmake一下,然后点击左下角的”构建“按钮,进行编译;最后,编译完成。此时会生成testplugin.dll和testplugin.lib两个库。

第四步:部署插件

编译完成后,在输出目录下,找到对应的testplugin.dll和testplugin.lib两个文件。分别拷贝到QT库的designer路径下和QT的IDE工具的designer路径下。

以我本地5.7.0路径为例:(具体以个人的QT安装路径为准)

QT库的designer库路径:C:\Qt\Qt5.7.1\5.7\msvc2013_64\plugins\designer

QT库的IDE路径:C:\Qt\Qt5.7.1\Tools\QtCreator\bin\plugins\designer
Here Insert Picture Description

第五步:测试使用插件

新建一个工程,在工程使用该Test自定义插件。

1、打开*.ui,在左下角会出现我们前面编译的自定义插件,如下图:
Here Insert Picture Description
直接拖拽到ui界面。

2、在工程所在文件目录中,新建include文件夹,将前面的插件头文件test.h放在该include文件夹中。新建lib文件夹,将前面生成的testplugin.lib静态库放在此lib文件夹中。
Here Insert Picture Description
3、打开测试工程的*.pro文件。添加相应的include路径和lib路径。
添加以下内容:

1 LIBS += $$PWD/lib/testplugin.lib
2 INCLUDEPATH += $$PWD/include

4、切换到release编译模式,点击qmake,生成相应的makefile文件。并将前面生成的testplugin.dll动态库添加到release输出目录中。如下图:
Here Insert Picture Description
5、点击构建,此时会生成*.exe到release输出目录。
Here Insert Picture Description
此时。直接运行*.exe就可以使用该插件了。自定义插件的新建并使用到此算是OK了。

第六步:Qt自定义插件注意事项:

1:每个Qt库bin目录的designer可执行文件都是和该库同一个编译器编译的,可用,如果想要集成到Qt Creator中,则需要注意版本,一般在windows上的Qt Creator版本是MSVC的,则需要对应的Qt库也是MSVC编译的,库版本和编译器版本必须保持一致才能是顺利集成到Qt Creator的重要前提。

2:自定义控件的名称不能小写,否则拖过去的控件自动生成的默认名称和类名一样,会编译通不过。这个问题坑了我很久,造成自动生成的UI代码保存,一直没有怀疑,后面才发现自动生成的代码类名和实例名称一样,冲突导致的。

3:自定义控件类头文件引入,Qt5.7以下版本为#include <QtDesigner/QDesignerExportWidget> 以上版本为#include <QtUiPlugin/QDesignerExportWidget>

4:类名前必须加入 QDESIGNER_WIDGET_EXPORT 宏。否则集成到Qt Creator 中编译会报错。不加的话可以在设计器中加载,但是编译会报错。

5:如果将生成好的dll文件放到Qt库目录下的 plugins\designer 下,可以在 designer 中看到。放到Qt Creator下的 bin\plugins\designer 则可以集成到Qt Creator中。

6:将自定义控件的头文件、dll文件、lib(mingw编译器为.a)文件复制出来,放到include(可自己随便命名,我这里习惯用include)目录,将include目录放到项目的源码文件下,在使用了自定义控件的项目的pro文件中,增加两行 INCLUDEPATH += P W D / i n c l in d e L I B S + = PWD/include LIBS += PWD / the include / ***. Copy lib (mingw compiler for .a), so you can properly compile, but not run after the compilation is complete, you need custom control dll file corresponding to the executable file to the same directory , now it's done.

Special episode words: Most of the articles are introduced to the corresponding libraries and header files into the corresponding Qt installation directory folder, why put here to include a directory, as the project together? The individual is so understandable, together, each can easily be copied with the project runtime files to the same directory as the executable file, but do not forget the Qt library catalog to find correspondence from the runtime. And when the release code can also have a good reference.

7: Qt Creator version of the official website provides essentially MSVC version, if you need custom control integrated in the corresponding mingw Qt library in Qt Creator, Qt libraries need their own with the corresponding source code to compile Qt Creator.

Guess you like

Origin blog.csdn.net/QIJINGBO123/article/details/88687083
Recommended