4

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

vahancho
  • 20,808
  • 3
  • 47
  • 55
user1336326
  • 71
  • 1
  • 8

2 Answers2

8

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();
    ...
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
0

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();
    [..]
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • I tried, but if I click on a single cell the QString cellText returns me only that cell, I need all the cells in a single or in few strings better. – user1336326 Jan 21 '14 at 16:01