简体   繁体   English

如何在QT Creator中推广自定义小部件

[英]How to promote a custom widget in QT Creator

In qt 5.2.1 I've created some custom widgets, like a button. 在qt 5.2.1中,我创建了一些自定义小部件,比如按钮。 Traditionally there's two ways of doing this. 传统上有两种方法可以做到这一点。 You can either promote an existing widget. 您可以升级现有小部件。 And change/add functionality. 并更改/添加功能。 Or create a custom widget from scratch. 或者从头开始创建自定义小部件。 I've used the latter. 我用过后者。

However, in some cases I would like to use my custom widget, but change some of it's functionality by promoting. 但是,在某些情况下,我想使用我的自定义小部件,但通过推广更改其中的一些功能。 The usual way to do this would be to add a widget, and promote it. 通常的方法是添加一个小部件,然后进行推广。 However, when creating a new kind of promoted widget, a base class has to be picked. 但是,在创建新类型的提升窗口小部件时,必须选择基类。 And in the dialog where this can be done, only the default widgets are listed. 在可以执行此操作的对话框中,仅列出默认小部件。

Is it possible to add a custom widget to this list? 是否可以将自定义小部件添加到此列表中?

Regards, 问候,

Lauris Lauris

Edit: 编辑:

I've played around with it a lot. 我玩过很多次。 And now all of a sudden a custom widget is been added to the list of base classes. 现在突然间,一个自定义小部件被添加到基类列表中。 Yet I still don't know how I've added it. 但我仍然不知道我是如何添加它的。 And why this is the only custom widget showing on the list. 为什么这是列表中唯一显示的自定义小部件。

Let's say we need to create custom button class, eg MyButton . 假设我们需要创建自定义按钮类,例如MyButton It inherits QPushButton , reimplements some of its methods and adds new methods. 它继承了QPushButton ,重新实现了它的一些方法并添加了新的方法。 Now we need to add it to a Qt Designer form. 现在我们需要将它添加到Qt Designer表单中。 We add a push button to the form and promote it to the MyButton class. 我们在表单中添加了一个按钮,并将其提升为MyButton类。 What does this action mean? 这个动作意味着什么?

  • The Ui class generated by the Qt Designer will have a member of type MyButton* . Qt Designer生成的Ui类将具有MyButton*类型的成员。 You can use ui->myButton (if you name it so) to access the object and use all its methods. 您可以使用ui->myButton (如果您这样命名)来访问该对象并使用其所有方法。 Of course all QPushButton 's methods will be also available because that's what C++ inheritance means. 当然,所有QPushButton的方法也都可用,因为这就是C ++继承的含义。
  • Qt Designer will know that MyButton inherits QPushButton and allow you to edit its specific properties (eg checkable ) and access its slots (eg clicked ). Qt Designer将知道MyButton继承QPushButton并允许您编辑其特定属性(例如checkable )并访问其插槽(例如, clicked )。

Now we want to extend MyButton functionality for some special case. 现在我们想针对某些特殊情况扩展MyButton功能。 We create a class named MySpecialButton derived from MyButton and slightly change its behavior. 我们创建了一个名为MySpecialButton的类,该类派生自MyButton并略微改变其行为。 How do we add it to a form? 我们如何将其添加到表单中? Just like the one above. 就像上面那个。 We add a push button to the form and promote it to the MySpecialButton class. 我们在表单中添加一个按钮,并将其提升为MySpecialButton类。 Qt Designer doesn't really care if QPushButton is the direct base of MySpecialButton or there is a long chain of inheritance. Qt Designer并不关心QPushButtonMySpecialButton的直接基础还是有很长的继承链。 It will still allow to edit its properties specific to push buttons, and you will still have full access to MySpecialButton methods through ui variable. 它仍然允许编辑特定于按钮的属性,并且您仍然可以通过ui变量完全访问MySpecialButton方法。

QPushButton is just an example. QPushButton只是一个例子。 All of above will be true even if you promote QWidget to MySpecialButton in the form. 即使您在表单中将QWidget提升为MySpecialButton ,上述所有内容都将成立。 The only difference it would make is that Qt Designer will not allow to edit QPushButton properties and access its slots, limiting you to QWidget 's properties. 它唯一的区别是Qt Designer不允许编辑QPushButton属性并访问它的插槽,限制你使用QWidget的属性。 On the other side, you are not required to derive MyButton from QPushButton . 另一方面,您不需要从QPushButton派生MyButton You can choose QWidget or any its subclass as base. 您可以选择QWidget或其任何子类作为基础。 It is only reasonable to choose the class with closest functionality, so that you'll need to implement less. 选择具有最接近功能的类是合理的,因此您需要实现更少的功能。 And it's reasonable to select the most direct base class before promoting because you'll be able to access more properties. 在推广之前选择最直接的基类是合理的,因为您将能够访问更多属性。 It's only a matter of convenience. 这只是方便的问题。 (But of course if you select wrong base class for promotion, it won't work.) (但是当然如果你为促销选择了错误的基类,它将无法工作。)

This seems to be a bug in Qt Creator. 这似乎是Qt Creator中的一个错误。 A workaround, oddly enough is to add widgets twice to the list of widgets a widget library exports: 一种解决方法,奇怪的是将小部件两次添加到小部件库导出的小部件列表中:

IOSwidgets::IOSwidgets(QObject *parent)
    : QObject(parent)
{

    m_widgets.append(new aircraftAttitudePlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new alfabeticKeypadPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new arrowedFramePlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new buttonWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new dataButtonPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new frameWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new headingDialPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new imageWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new labelWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new linkedButtonWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new linkedLabelWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new linkedSliderWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new NavButtonPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new numericKeypadPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new repositionWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new runwayInformationPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new slewWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new sliderWidgetPlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new WeightBalancePlugin(this));
    m_widgets.append(m_widgets.last());
    m_widgets.append(new WindshearMicroburstLateralPlugin(this));
    m_widgets.append(m_widgets.last());
}

Hope this saves the next person who runs into this problem, the painstaking effort and the huge amount of time it took me to find this out:). 希望这可以节省下一个遇到这个问题的人,我付出了艰苦的努力和花费了大量的时间来找到这个问题:)。

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

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