How to promote a custom widget in QT Creator

前端 未结 2 1819
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 07:13

In qt 5.2.1 I\'ve created some custom widgets, like a button. Traditionally there\'s two ways of doing this. You can either promote an existing widget. And change/add functi

相关标签:
2条回答
  • 2021-01-13 07:20

    Let's say we need to create custom button class, e.g. MyButton. It inherits QPushButton, reimplements some of its methods and adds new methods. Now we need to add it to a Qt Designer form. We add a push button to the form and promote it to the MyButton class. What does this action mean?

    • The Ui class generated by the Qt Designer will have a member of type MyButton*. You can use ui->myButton (if you name it so) to access the object and use all its methods. Of course all QPushButton's methods will be also available because that's what C++ inheritance means.
    • Qt Designer will know that MyButton inherits QPushButton and allow you to edit its specific properties (e.g. checkable) and access its slots (e.g. clicked).

    Now we want to extend MyButton functionality for some special case. We create a class named MySpecialButton derived from MyButton and slightly change its behavior. 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. Qt Designer doesn't really care if QPushButton is the direct base of MySpecialButton or there is a long chain of inheritance. 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.

    QPushButton is just an example. All of above will be true even if you promote QWidget to MySpecialButton in the form. 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. On the other side, you are not required to derive MyButton from QPushButton. You can choose QWidget or any its subclass as base. 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.)

    0 讨论(0)
  • 2021-01-13 07:40

    This seems to be a bug in 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:).

    0 讨论(0)
提交回复
热议问题