PDA

View Full Version : Is it possible to map data in UserRoles through QDataWidgetMapper?



NameRakes
13th April 2021, 02:51
I have a tree model instanced from QStandardItemModel with exactly 1 column (name of the item) for each item in the tree. Leaf level items can have different sets of data, and in the model I use Qt::UserRole + n to store them, hoping this is more efficient than having 'n' columns in each item.

Essentially the tree view shows only the item names which also double up as DisplayRole and EditRole. When an item is selected in QTreeView I would like to show the "User role data" in standard widgets like QLineEdit etc. inside a layout.

I can shuttle the data between the model and the widgets manually like so:

QObject::connect(qTreeView->selectionModel(),
&QItemSelectionModel::currentRowChanged,
myClassInstance, &MyClass::processCurrentIndex);
Then populate the widgets with the data from the user roles, and update the model when each editor is finished. Can be done this way.

But, is it possible to do it more easily? Say, by mapping "User role data" to widgets using QDataWidgetMapper? If so, how? It doesn't appear that I can use QDataWidgetMapper::addMapping() to do that since int section refers to column in the item (see https://doc.qt.io/qt-5/qdatawidgetmapper.html#addMapping).

ChrisW67
14th April 2021, 03:22
You might be able to do this with the QDataWidgetMapper::itemDelegate() . It is responsible for marshalling data to/from the widgets so it should be able to use an alternate role as the source and destination. Might take a bit of experimentation.

NameRakes
14th April 2021, 12:55
Thanks for your response. I should have mentioned that I did think of creating my delegate for the data mapper (I have one for QTreeView). But after some analysis, I abandoned the idea. My tree view and to its right, the GUI/dialog with its 3 widgets, looks like this.

- Files | wFile: file2
|--file1 |
|--file2 | wUR1: xyz
|--file3 | wUR2: abc

The column count for Files index is 1, and row count is n, but column count for individual files is 0! Let's say each file file1, file2, ... has user role data ur1, ur2. The mapper and its delegate dFile attached to the widget wFile is for the selected file, but not the 2 widgets, say, wUR1, wUR2, that I will have for the user role data. I could try to use dFile to shuttle the data of wUR1, wUR2 back and forth from the model. I may be wrong but I think it can become challenging to connect dFile to editors of wUR1, wUR2.

So I thought I would derive MyUserRoleMapper from QDataWidgetMapper like so:


class MyUserRoleMapper : public QDataWidgetMapper { ... };
MyUserRoleMapper::addURmapping( QWidget* wUR, UREnum urEnum ); // and use as follows

myURmapper.addURMapping( wUR1, urEnum1 );
myURmapper.addURMapping( wUR2, urEnum2 );

The derived class will maintain the above mapping, and then whenever the current model index from the base class QDataWidgetMapper changes, populate data into user role widgets. Likewise when these widgets are edited, based on the editor losing focus, shuttle the data back to the model. I don't think I need a custom delegate here.

Would this work? Appreciate any feedback you may have. Thanks.