Results 1 to 6 of 6

Thread: Programatically add USerRole data to specific cell of QAbstractTableMode

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Programatically add USerRole data to specific cell of QAbstractTableMode

    Quote Originally Posted by d_stranz View Post
    QAbstractTableModel does not re-implement setData(), so it is using the implementation from its base class, QAbstractItemModel. The documentation for QAbstractItemModel::setData() says:



    So if you have not implemented setData() in your own model (derived from QAbstractTableModel), then using the default setData() method is doing nothing to store your string.

    This tutorial would be a good place to start. There is a section on creating an editable model.
    Sorry I had misunderstood your previous message.
    Yes I implemented a setData from my model.

    I write it below:

    Qt Code:
    1. bool SelectedListModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3. if (!index.isValid())
    4. {
    5. return false;
    6. }
    7. if (role == Qt::EditRole)
    8. {
    9. m_reports[index.row()].replace("actionName", value.toString());
    10.  
    11. emit dataChanged(index, index);
    12. return true;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    It is not clear to me how to add data with a custom role.
    Can you help me with a small example?
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Programatically add USerRole data to specific cell of QAbstractTableMode

    It is not clear to me how to add data with a custom role.
    All you have to do is define a new role with the value > Qt:: UserRole, then call setData() with the string you want to add and the new role.

    In the code you have posted above, the only time you save anything is when the role == EditRole. If you want setData() to save your string when the role == UserRole + 1, you must add an if () clause for that.

    QTableView does not know anything about user roles so it will never ask for it, but you can call data() on your model (or the model index) with this role whenever you want to get the value for your custom role.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    franco.amato (27th November 2021)

Similar Threads

  1. Replies: 15
    Last Post: 15th August 2016, 22:08
  2. Disable a specific cell in QStandardItemModel
    By Omid123 in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2015, 12:52
  3. Replies: 1
    Last Post: 12th May 2013, 21:17
  4. Reformat data in cell
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2009, 08:21
  5. setting UserRole data in QSqlTableModel
    By orgads in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 09:40

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
  •  
Qt is a trademark of The Qt Company.