6

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 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

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.

laurisvr
  • 2,724
  • 6
  • 25
  • 44
  • Hi, with Qt Creator, it's not becauses ONLY base class are displayed and can be picked that you can't write your own custom class. – Martin Sep 05 '14 at 11:19
  • Promoting is just a simple way to add and display a custom widget in Qt Designer. It does nothing with functionality. – Ezee Sep 05 '14 at 11:21
  • @Ezee Promoting is a way to add a custom widget based on an already existing widget(base class). I'd like for this widget to be a widget i've defined myself. Rather then an existing widget like a QButton or QSlider. – laurisvr Sep 05 '14 at 11:29
  • @Martin QT is opensource, so it should be possible to edit what is displayed in this list by changing the source code. However I don't think this is a necessity. Because when i build my own qt creater, some of the widgets I've created are added to this list. However, some are not. I don't see the logic in which widgets are added and which are not. – laurisvr Sep 05 '14 at 11:33
  • I don't know how to do exactly what you want but i'm also not sure if it's really necessary in your case. When using custom widget, well, it's quite simple to do it via your source code. Don't you know how to do it or is it really necessary for you to promote it ? – Martin Sep 05 '14 at 11:39
  • @Martin I'm building quite a big application. And i've build my own button widget. However, some buttons require little tweaks. Adding all these tweaks to the source code of the custom widget is possible indeed. I don't think it's desirable though. For both performance and maintainability reasons. The huge advantage of promotion is that you can do little tweaks for very specific cases:) – laurisvr Sep 05 '14 at 11:44

2 Answers2

1

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

laurisvr
  • 2,724
  • 6
  • 25
  • 44
  • I have the same problem, but I did not understand what you did exactly. Could you please give me more details? – Behzad Dec 20 '20 at 11:11
  • It's been a while:), but there's nothing more too it than what I said here. In order for the widget to show up you need to add it twice to the list of widgets being loaded by qtcreator. They might have fixed the bug by now though – laurisvr Dec 20 '20 at 11:23
  • Thanks for your prompt reply. Actually, I am working with Qt Creator on Windows and my specific question is that where I can modify the list you mentioned? – Behzad Dec 20 '20 at 12:06
  • 1
    Well from what I remember, in order to add your own widgets you need to compile Qt creator yourself. Somewhere in Qt creator's code the widgets are being loaded, there you can add your own list. – laurisvr Dec 20 '20 at 12:43
  • Ok, I used the Qt Design Custom Widget module in order to do that, so I have not compiled At creator from the scratch. – Behzad Dec 20 '20 at 21:09
0

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

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • Hi Pavel, Thank you for your answer. It sheds some really good light on how promotion and promotion inheritance works. However in my case I don't want to use inheritance. But I want to promote a custom widget, I've added to QT creator. Do you happen to know how that would work? – laurisvr Sep 08 '14 at 07:45
  • There can't be widgets without any inheritance. They must inherit `QWidget` somehow. If you have a custom widget that inherits `QWidget` directly, you can add a Widget to the form and promote it to the custom widget as usual. – Pavel Strakhov Sep 08 '14 at 11:36
  • My custom widget inherits from qwidget. But rather then using promotion i've created a widget plugin like shown in this [link](http://qt-project.org/doc/qt-4.8/designer-customwidgetplugin.html) example . This way I've also added properties and stuff like that. But if i were to promote a QWidget these properties wont be accessible. So say I've named my custom widget MySpecialButton, i'd like to promote this widget. – laurisvr Sep 08 '14 at 11:43
  • Thanks @Pavel, this explains promotions very clearly. [Relevant Qt documentation here.](http://doc.qt.io/qt-4.8/designer-using-custom-widgets.html#promoting-widgets) – Carlton Oct 03 '16 at 16:15