Question

I have a QTableView with few records, a single row contains four columns. I need to get these 4 index values (name, surname, age, username) in order to delete them in SQLite, so I need these four values to put in the deletion query. I expect to click on an every index of THAT row and get back all the 4 values. How can I do it? Thanks

Was it helpful?

Solution

I don't see a problem. With QModelIndex you can get any data relative to given model index.

void GuiClass::onTableCellClicked(const QModelIndex &index)
{
    int row = index.row();
    QString name = index.sibling(row, 0).data().toString();
    QString surname = index.sibling(row, 1).data().toString();
    int age = index.sibling(row, 2).data().toInt();
    QString username = index.sibling(row, 3).data().toString();
    ...
}

OTHER TIPS

First you need to handle clicks on your table view. For that purpose you can handle QAbstractItemView::clicked(const QModelIndex &index) signal and connect it to the appropriate slot. For example:

void GuiClass::onTableCellClicked(const QModelIndex &index)
{
    QString cellText = index.data().toString();
    [..]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top