简体   繁体   English

如何将自定义小部件添加到Qt 4.8.6

[英]how to add a custom widget to Qt 4.8.6

I'm trying to figure out how to add a custom widget to Qt Designer. 我试图弄清楚如何向Qt Designer添加自定义窗口小部件。 So far I've been unsuccessful. 到目前为止,我一直没有成功。 Very new to Qt. Qt的新手。 I'm running Qt 4.8.6. 我正在运行Qt 4.8.6。 I know there is a newer version but due to project constraints I have to stick with this one. 我知道有一个更新的版本,但是由于项目的限制,我不得不坚持使用这个版本。 Essentially I'm need to add a hex spin box, ie a spinbox that counts in hex rather than in decimals. 本质上,我需要添加一个十六进制旋转框,即一个以十六进制而不是十进制计数的旋转框。 I even found code that implements it . 我什至找到实现它的代码 It does explain how to integrate it into Qt Designer , however it is explained for version 3. Can someone offer assistance? 它确实说明了如何将其集成到Qt Designer中 ,但是针对版本3进行了说明。有人可以提供帮助吗?

Any help is greatly appreciated 任何帮助是极大的赞赏

Read about creating designer widgets and using them , but briefly: 阅读有关创建设计器小部件使用它们的信息 ,但简要介绍一下:

  • Subclass QDesignerCustomWidgetInterface to create your custom designer plugin for your custom widget 子类QDesignerCustomWidgetInterface为您的自定义小部件创建自定义设计器插件
  • Build and install your designer plugin 生成并安装设计器插件
  • Tell designer about your plugin location, or just make sure it's installed to $QTDIR/plugins/designer 告诉设计者您的插件位置,或者只是确保将其安装到$QTDIR/plugins/designer

There's also an example to follow. 还有一个示例可以遵循。

Here is a documentation. 是一个文档。 At short, you just add any widget (eg QSpinBox to draw it nice in the Designer) to your form and convert it to the needed one. 简而言之,您只需将任何小部件(例如QSpinBox即可在Designer中将其绘制得漂亮)添加到表单中,然后将其转换为所需的小部件。

To add a custom widget to Qt designer simply follow the steps: 要将自定义窗口小部件添加到Qt设计器,只需执行以下步骤:

  1. In Qt designer create a new widget File->New->Widget. 在Qt设计器中,创建一个新的小部件File-> New-> Widget。
  2. Add your UI etc for this widget and save it->->MyWidget.ui 为此小部件添加您的UI等并保存它->-> MyWidget.ui
  3. Create a class "MyWidgetHandler" to handle this widget in a MyWidgetHandler.h & cpp 创建一个类“ MyWidgetHandler”以在MyWidgetHandler.h和cpp中处理此小部件
  4. In your MainWindow/Dialog wherever you want to display this widget add a "Widget" from the "Containers" section of the designer. 在要显示此小部件的任何位置的MainWindow / Dialog中,从设计器的“容器”部分添加“小部件”。

    在此处输入图片说明

  5. Now right-click the "Widget" container you just added and click "Promote to..." 现在,右键单击刚刚添加的“窗口小部件”容器,然后单击“升级为...”

  6. This should open a dialog. 这应该打开一个对话框。 You can select here the base class to inherit properties in your case QComboBox or just select the QWidget class. 您可以在此处选择基类以继承您的案例QComboBox中的属性,也可以仅选择QWidget类。
  7. Next enter the full class name ie with namespaces eg: "blah::MyWidgetHandler" 接下来输入完整的类名称,即带有名称空间,例如:“ blah :: MyWidgetHandler”
  8. In the Header file section simply add the path to the header file for this class. 在“头文件”部分中,只需将路径添加到此类的头文件中。 This should be a resolvable path. 这应该是一个可解决的路径。 Eg: "UI/MyWidgetHandler.h" or "C:/UI/MyWidgetHandler.h" 例如:“ UI / MyWidgetHandler.h”或“ C:/UI/MyWidgetHandler.h”
  9. Now click "Promote" and save your MainWindow.ui 现在单击“升级”并保存您的MainWindow.ui
  10. The Property Editor should now show your custom class type eg: blah::MyWidgetHandler. 现在,属性编辑器应显示您的自定义类类型,例如:blah :: MyWidgetHandler。 and the properties from the base class you selected. 以及所选基类的属性。
  11. Don't forget to setup the UI in your handler class and include header in the Mainwindow handler. 不要忘记在处理程序类中设置UI,并在Mainwindow处理程序中包含标头。

Cheers !!! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM