QTableView Question

  • I have a tableview and am loading the model based from data from a database. That is why I used the view instead of a tablewidget. The challenge is I can't seem to be able to get a particular cell value from the view. What I am trying to accomplish with the View is essentially this code in the TableWidget. The user selects a cell and I want to grab the string data. Seems simple but for some reason I can't seem to figure it out.

    Here is the equivalent code for the TableWidget I am trying to accomplish.
    QList<QTableWidgetItem*> selectedRow = ui->tblRemoveTableLogList->selectedItems();
    QString cellValue = selectedRow.at(0)->text();
    return cellValue.toInt();

    Any help is appreciated.

  • The Challenge is setModel() in QTableWidget is private and I need to set the model from the Database. Any ideas?

  • Lifetime Qt Champion


    You can use the QSelectionModel::selectedIndexes and from there get the text.

    Hope it helps

  • I was trying that but could not seem to get the data out of the QModelIndexList. Where is the actual data stored in this class? Or how do I access it? I believe this list is a 1-N rows of data selected, but can't seem to get to a specific cell data.

  • Hi,

    When the selection model is changed, you can be notified through the signal QItemSelectionModel::selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected )

    Then, you access selected indexes with QItemSelection::indexes()

    @QModelIndexList indexes = theItemSelection.indexes();
    foreach (QModelIndex index, indexes)
    QString selectedItemAsString = index.data().toString();

  • Thanks Seb. I must be missing something because there is no signal selectionChanged in QTableView

  • selectionChanged() is inherited from QAbstractItemView


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.