Results 1 to 6 of 6

Thread: Programatically add USerRole data to specific cell of QAbstractTableMode

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    655
    Thanks
    42
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Programatically add USerRole data to specific cell of QAbstractTableMode

    Hi Qt lovers,
    I am still in the learning phase of Model/View...

    I have a class derived from QAbstractTableModel where I would add a QString to a specific cell at a specific time.

    Internally the data of the model is stored in a QVector called m_reports but additionally I need to store some data that I need to retrieve from the MainWindow in a doubleclick event.

    I though that setData is the correct function for that but I can't get it to work and I don't understand why.

    Below the function that feed the model data

    Qt Code:
    1. void SelectedListModel::appendReport(const QMultiMap<QString, QString> &report)
    2. {
    3. int row = m_reports.count();
    4. beginInsertRows(QModelIndex(), row, row);
    5. QString xmlFile = report.value("xmlFilePath");
    6. qDebug() << xmlFile; // xmlFile contains right value
    7. setData(index(row, 0), QVariant(xmlFile), Qt::UserRole + 1); // <-- this does not work, no data is written
    8. m_reports.append(report);
    9. endInsertRows();
    10. }
    To copy to clipboard, switch view to plain text mode 

    In my MainWindow class, I catch the double-clicked event over the table view and I would get the value written with setData

    ?
    Qt Code:
    1. connect(ui->selectedItemsView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(showConfigureDialog(const QModelIndex &)));
    To copy to clipboard, switch view to plain text mode 

    And the relative slot from where I would get the value I need to process

    Qt Code:
    1. void MainWindow::showConfigureDialog(const QModelIndex &index)
    2. {
    3. if (index.column() == 1)
    4. {
    5. return;
    6. }
    7.  
    8. const QAbstractItemModel *model = index.model();
    9. QString xmlFilePath = model->data(model->index(index.row(), 0), Qt::UserRole+1).toString();
    10. qDebug() << "Cell Value: " << xmlFilePath; // This gives an empty output
    11. }
    To copy to clipboard, switch view to plain text mode 

    I can not understand what can be the problem.
    I hope to get some help.

    Franco
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,074
    Thanks
    287
    Thanked 834 Times in 821 Posts
    Qt products
    Qt5
    Platforms
    Windows

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

    What do you get if you call QModelIndex::data() directly instead of going through the model?

    Does your setData() method actually save the string someplace for retrieval later?

    Not really sure why you are calling your model to create a new QModelIndex when the one passed in has everything you want, especially after you have eliminated indexes with the wrong column.
    <=== 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. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    655
    Thanks
    42
    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
    What do you get if you call QModelIndex::data() directly instead of going through the model?

    Does your setData() method actually save the string someplace for retrieval later?
    Hi,
    It's not my method, it's the QAbstractTableModel::setData(...)

    Quote Originally Posted by d_stranz View Post
    Not really sure why you are calling your model to create a new QModelIndex when the one passed in has everything you want, especially after you have eliminated indexes with the wrong column.
    I don't know
    obviously I made a mistake. It's possible to have few lines of code to clarify my doubts please?


    Added after 5 minutes:


    Quote Originally Posted by d_stranz View Post
    What do you get if you call QModelIndex::data() directly instead of going through the model?
    Adding:
    Qt Code:
    1. qDebug() << index.data();
    To copy to clipboard, switch view to plain text mode 

    I got QVariant(Invalid)
    Last edited by franco.amato; 26th November 2021 at 12:05.
    Franco Amato

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,074
    Thanks
    287
    Thanked 834 Times in 821 Posts
    Qt products
    Qt5
    Platforms
    Windows

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

    QAbstractTableModel does not re-implement setData(), so it is using the implementation from its base class, QAbstractItemModel. The documentation for QAbstractItemModel::setData() says:

    The base class implementation returns false. This function and data() must be reimplemented for editable models.
    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.
    <=== 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.

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    655
    Thanks
    42
    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

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,074
    Thanks
    287
    Thanked 834 Times in 821 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.

  7. 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, 23:08
  2. Disable a specific cell in QStandardItemModel
    By Omid123 in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2015, 13:52
  3. Replies: 1
    Last Post: 12th May 2013, 22:17
  4. Reformat data in cell
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2009, 09:21
  5. setting UserRole data in QSqlTableModel
    By orgads in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 10: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.