Using Qt Model / View with custom data and non-tabular / list?

I read about the structure of Qt Model / View. I find this to be really useful when dealing with related data, such as tables from a database. My question is: will it be useful for data other than a table, such as a list of properties or just multiple data sets of different types? If so, how can I approach him?

The goal is to come up with an editor for some list of properties, such as data. The list is created at runtime, and the elements are of different types (numbers, lines, logical and file paths, to name a few). Each element is basically a name-value pair. The name, type and restrictions (limits, for example) for each element are determined at compile time. They will be collected at runtime in different lists, depending on user input. And the list of items may change during the editing session.

The user interface will most likely consist of a combination of predefined widgets assembled according to user input. They may not appear in a list or table.

Some design template pointers or examples are also much appreciated. Thanks.

+6
source share
2 answers

I do not see a problem with the MVC card in QT for this.

Basically, the difference between the standard table display and the fact that you create a list that looks like a map dynamically:

QMap<QString, QVariant> property_map; 

You can do:

 QList<std::pair<QString, QVariant>> property_list; 

which can then be used to display properties in the table. A better way would probably be:

 struct { QString prop_name; int prop_type; QVariant prop_value; }; 

QVariant will basically provide you with one abstraction class for storing data, and in fact this is what is returned by the data() function inside QAbstractItemModel , which you could override.

So, basically you take a list of properties and boil it in the same table as the data in the form of a database.

CHANGED

If you have a widget that you want this widget to be filled with other predefined widgets, you will most likely have many problems if the widgets do not have the same or well-defined size.

What you can do is in widgets. A widget defines a layout, for example: QGridLayout or other possible layouts, then add other widgets to it it uses some set of parameters that can be executed, but can be a little painful.

Another approach you can take is to place all the widgets on the front panel of the display interface and simply include the ones you need and the rest, but this only applies if you have a well-defined limited number of presets, designed widgets .

+3
source

I have been using the Model / View structure for a long time, and usually I use my own models with a backend based on Qt containers (vectors, list, etc.). Even if the data ultimately comes from the database, working with (for example) the vector of database identifiers can significantly improve performance (and sometimes this is the only way you can do this).

This trivial example from Qt docs (see “Creating a Custom Model”) is a starting point and shows how to use the QStringList as a backend for a custom model.

After defining your model, you can define your custom views that will draw the placed widgets based on the contents of the model below it. When the model changes, your view will change, accordingly changing the widgets when necessary. Using the capabilities of QVariant , you should be able to display the corresponding widget for each data type (for example, QSpinBox for float QComboBox for QStringList and so on ...)

+2
source

Source: https://habr.com/ru/post/910607/


All Articles