Search code examples
qtuser-interfacemodelwidgetdesigner

Qt custom widgets with custom items - Qt Designer/ uic


I have started working with Qt again. What we want to do is to automatically generate graphical user interfaces. Therefore I have taken a closer look at the .ui file format (which is a XML based file format). These files are generated by the Qt Designer but can be easily be created by any other software. This part works so far.

But: we want to add some custom widgets. Creating custom widgets is also not that difficult. Some good websites exist out there: Creating Custom Widgets for Qt Designer, Using Custom Widgets with Qt Designer, (more hyperlinks held back),...

My question is rather: how can you implement custom widgets displaying custom items. Let's take one example. We have a QComboBox and a QSpinBox. The combobox has three items

  • A
  • B
  • C.

When selecting "A" I want the spinbox' minimum set to 1, maxiumum set to 9; when selecting "B" the spinbox' minimum should be set to 10, and its' maximum to 99; selecting "C" should set the spinbox' minimum to 100 and the maximum to 999. How is this done properly?

At the moment I set the data in a QTableWidget which is a helper class for me. Each row has three columns/cells:

item |  min | max
-----+------+-----
  A  |   0  |   9
  B  |  10  |  99
  C  | 100  | 999

And I have my custom widget which contains the QComboBox and the QSpinBox. At runtime I tell the custom widget to take data/model from the QTableWidget and copy to itself by a method called setFriendContainer() [it's even a public slot which should be connected to a signal from the tablewidget which is emitted automatically]. Afterwards the tablewidget is not needed any more! I don't even want to show it to the user at all (this is why some size properties are set to 0; but it's partly still visible).

I think this is not really a clean way to implement it. What would be better/ an alternative? The items are also generated and should be contained in the same .ui file. Of course it would be possible to load it at runtime from another file, but I think this isn't rather an option. Do you think my workaround is ok, or is it too "ugly"? There are some examples where you can see how to add single custom properties, but I think this is something different - if you want to add whole containers (lists or tables of items, whatever). But including it in the .ui files might even be against the whole model/view concept?!

Any help welcome!

Thanks, Matthias


Solution

  • I have added our data in a custom property in the form of XML. The data stores some coding information used in the widget. It's good to have the dynamic properties.