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
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?
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.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.)
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:).