Results 1 to 3 of 3

Thread: Is it possible to map data in UserRoles through QDataWidgetMapper?

  1. #1
    Join Date
    Dec 2016
    Location
    New England, US
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Is it possible to map data in UserRoles through QDataWidgetMapper?

    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).

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to map data in UserRoles through QDataWidgetMapper?

    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.

  3. #3
    Join Date
    Dec 2016
    Location
    New England, US
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Is it possible to map data in UserRoles through QDataWidgetMapper?

    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:
    Qt Code:
    1. class MyUserRoleMapper : public QDataWidgetMapper { ... };
    2. MyUserRoleMapper::addURmapping( QWidget* wUR, UREnum urEnum ); // and use as follows
    3.  
    4. myURmapper.addURMapping( wUR1, urEnum1 );
    5. myURmapper.addURMapping( wUR2, urEnum2 );
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by NameRakes; 14th April 2021 at 13:18.

Similar Threads

  1. Replies: 2
    Last Post: 1st March 2014, 16:09
  2. Replies: 1
    Last Post: 6th January 2014, 15:30
  3. QDataWidgetmapper
    By locus in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2012, 11:24
  4. Replies: 1
    Last Post: 29th January 2011, 13:14
  5. Replies: 3
    Last Post: 21st January 2008, 12:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.