0

In a QTableView, I have few user data that is associated with some QStandardItem,

That every row has one user data (I use row selection mode)

Now when user right click on any item on the same row, they get the same data.

So my problem is, I set the user data on the first column of every row, every time I got a click event, I need to find the item on the same row, and the first column first, then lookup the associated user data.

That looked fairly stupid, is it possible to set data for the whole row?

daisy
  • 22,498
  • 29
  • 129
  • 265

1 Answers1

1

You may consider to have a custom QAbstractTableModel with a list of your data. I currently have a project with requirement similar to you and subclassing QAbstractTableModel work for me.

In your QAbstractTableModel, create a method to return an item of your data by row, like DataClass* getRecord(int row); and in your QTableView row click event, associate the method with selected row.

void MyTable::showEditDialog()
{
    MyModel* m = qobject_cast<MyModel*>(model());
    QModelIndexList selected = selectionModel()->selectedIndexes();
    MyDialog dialog(m->getRecord(selected[0].row()), this);
    if (dialog.exec() == QDialog::Accepted)
    {
        m->refresh(selected[0]);
        Q_EMIT contentEdited();
    }
} // end_slot(MyTable::showEditDialog)

Check here and here for documentation of QAbstractTableModel and model-view-programming.

YamHon.CHAN
  • 866
  • 4
  • 20
  • 36