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:
Qt Code:
  1. QObject::connect(qTreeView->selectionModel(),
  2. &QItemSelectionModel::currentRowChanged,
  3. myClassInstance, &MyClass::processCurrentIndex);
To copy to clipboard, switch view to plain text mode 
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 http://doc.qt.io/qt-4.8/https://doc.qt.io/qt-5/qdatawidgetmapper.html#addMapping).