How to promote custom widget in QT Creator

In qt 5.2.1 I have created some custom widgets like a button. Traditionally, there are two ways to do this. You can either promote an existing widget. And change / add functionality. Or create your own widget from scratch. I used the latter.

However, in some cases I would like to use my custom widget, but changing some of its functionality, promoting. The usual way to do this is to add a widget and promote it. However, when creating a new type of promoted widget, you must select a base class. And in the dialog where it can be done, only the default widgets are listed.

Can I add a custom widget to this list?

Hello,

Lauris

Edit:

I played with him a lot. And now, unexpectedly, a custom widget has been added to the list of base classes. But I still don't know how I added it. And why is it the only custom widget shown in the list.

+3


source to share


2 answers


This seems to be a bug in Qt Creator. A workaround, oddly enough, is to add the widgets twice to the list of widgets that the 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());
}

      



Hopefully this saves the next person who encounters this problem, painstaking effort and a huge amount of time it took me to figure it out :).

+1


source


Let's say we need to create a custom button class eg. MyButton

... It inherits QPushButton

, reuses some of its methods, and adds new methods. Now we need to add it to the Qt Designer form. We add a button to the form and promote it to the class MyButton

. What does this action mean?

  • The Ui class generated by Qt's constructor will be of type MyButton*

    . You can use ui->myButton

    (if you call it that) to access an object and use all of its methods. Of course, all methods QPushButton

    will also be available as that means what C ++ inheritance means.
  • Qt Designer will know what it MyButton

    inherits QPushButton

    and allows you to edit its specific properties (for example checkable

    ) and access its slots (for example clicked

    ).


Now we want to extend the functionality MyButton

for some special case. We create a class named MySpecialButton

derived from MyButton

and slightly change its behavior. How do I add it to the form? As above. We add a button to the form and promote it to the class MySpecialButton

. Qt Designer doesn't care if it QPushButton

is a direct base MySpecialButton

or if there is a long inheritance chain. It will still allow you to edit its button-specific properties, and you still have full access to the MySpecialButton

methods via the variable ui

.

QPushButton

is just an example. All of the above is true, even if you are promoting QWidget

in MySpecialButton

the form. The only difference it would make is that Qt Designer will not allow you to edit properties QPushButton

and access its slots, restricting you to properties QWidget

. On the other hand, you don't need to output MyButton

from QPushButton

. You can choose QWidget

or any of its subclasses as a base. It's wise to choose the class with the closest functionality, so you need to implement less. And it is wise to choose the most direct base class before promoting, because you will be able to access more properties. It's just a matter of convenience. (But of course, if you choose the wrong base class for your promotion, it won't work.)

+2


source







All Articles