Reputation: 1077
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
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
Upvotes: 0
Views: 914
Reputation: 1077
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.
Upvotes: 1
Reputation: 778
Remember that you are dealing with the properties of the widget. A UI file is nothing more than a description of the widgets, their properties, and their relation to each other in the hierarchy. A custom widget is no different than something like a QPushButton or QComboBox to Designer. The important thing is to encode the properties of your custom widget in a way that is understandable in the UI file.
You probably are best served by looking at the UI schema and making sure your widget can be described in a valid file. After that it's just to make sure that your properties behave correctly (i.e., that you can set properties in any order and get the same result). How you implement the widget is really up to you (make your own data structure, don't drag an "invisible" tree widget in).
Upvotes: 0