Unsolved Design problem between choice of single or multiple C++ models
-
Hello, I have currently C++ model holding list of names and associated data for those names. The list items can be checked by user. And the checked list items will be displayed in the mainwindow by circular gauge (if possible, ability to change the the view component on run time to another maybe gauge, etc).
I have confusion on how can I feed the circular gauges with a single C++ model or should I create separate C++ model for separate gauges? Please help
-
I have confusion on how can I feed the circular gauges with a single C++ model
I have got doubts that it can usually make sense to display the same data by more instances of the same view.
or should I create separate C++ model for separate gauges?
I would find it more useful to show different input data on similar output views.
How do you think about to indicate the desired connections by labels or specific colours? -
@elfring. What i meant to say is each gauge will have different data, because it is fed by different data in the list. Of course, I do know that same model can be used in multiple views. I do not know understand what you try to explain "How do you think about to indicate the desired connections by labels or specific colours?".
-
I do not know understand what you try to explain "How do you think about to indicate the desired connections by labels or specific colours?".
- You can display data also in a redundant way (if it is really desired).
- How do you distinguish if the used views should present different data from separate input sources?
-
@elfring. Yes I have these problems. I do not also know which data structure could be suitable for this.
I am thinking of QList<QVariant>. The QList item of QVariant will again hold QList of QVariants. I do not know if this is appropriate solution. -
@milan said in Design problem between choice of single or multiple C++ models:
What i meant to say is each gauge will have different data, because it is fed by different data in the list.
So basically each element in the list should be represented by a circular gauge. I that correct? if so, how are the gauges arranged on the view (one under the other, in a grid, etc.)
-
@VRonin . Yes, for testing, I am using horizontal layout now. Grid layout can be good option.
-
I am thinking of QList<QVariant>.
Are you using customised data models for your software application already?
I do not know if this is appropriate solution.
Which objects will you pass to the member function “setModel” of your views?
-
@elfring. Yes, I am using QAbstractListModel now. But I do not know if QAbstractItemModel or QAbstractTableModel would be better choice?
I am not yet able to have working application yet. I think I need to pass Qvariantlist for each gauge because gauge will have name, value and unit. And all those parameters will come from the model.
-
I am using QAbstractListModel now.
Would you like to fiddle with any “tables” after the current list approach?
-
@elfring. I got confused now with list based approach or table based approach as gauge should have few parameters coming from model namely, name, unit, and value.
-
few parameters coming from model namely, name, unit, and value.
Did you put these items into a specific class together?
-
Ok, let's split the problem in the 3 components:
The model
You can subclass
QAbstractListModel
and build your own but I suggest to useQStandardItemModel
through theQAbstractItemModel
interface only (the easier way is to have something likeQAbstractItemModel* model = new QStandardItemModel
.
Now you can useinsertRows
/insertColumns
to add gauges andsetData
to store in different roles all the data you need for the gauge (e.g. min, max, current value, tick distance, colour, etc.)The delegate
This is a
QStyledItemDelegate
subclass that will take care of painting a single gauge. The method you want to reimplement isQStyledItemDelegate::paint
. From there you can useindex.data(role)
to retrieve the data stored in the various roles of the model beforeThe View
This decides how the gauges are laid out you can use
QListView
/QTableView
in the beginning and then subclass your own of you feel the need
An Alternative
Instead of the delegate+view approach you can use
QDataWidgetMapper
http://doc.qt.io/qt-5/qdatawidgetmapper.html to map data in a model directly in a widget. This is not the most efficient solution however as it duplicates the data and it's really designed to show a specific item (or a subset of items) from the model -
@elfring . No, not yet. I thought of QList<QVariantList>. The QVariantList would contain QVariants of QString(Name), QString(Unit), double/int (Value). Just an idea now.
-
Just an idea now.
I suggest to reconsider the data structure design. The software dependencies might become clearer also for your use case.
-
@VRonin. In C++, we are not sure how many gauges will be in the window. The gauges will be added depending upon the parameters chosen by user in the UI. For example, if the user choses 5 parameters, there will be 5 gauges.
-
@milan said in Design problem between choice of single or multiple C++ models:
For example, if the user choses 5 parameters, there will be 5 gauges.
You are free to call
insertRow
orremoveRow
on the model at runtime to change the number of gauges -
@elfring . Yes, I may need to rethink the datastructure. But it is the best I can think of right now.
-
@VRonin. Okay, I can also give your idea a try. But what about the gauge parameters like Name, value, unit that is coming from the model. I also forget to add that gauge is Circular gauge from QtQuick2 extras used as quickwidget in QtWidget application.
-
But it is the best I can think of right now.
Would you like to combine any more elements into specific classes?