Frederik Ubben
Frederik Ubben

Reputation: 143

QTableView : how to select data?

I have a hopefully simple question, I will explain it by showing you a screenshot and a snippet of my source code:

enter image description here

I want to select a cell like wo_forename or wo_name, at the moment I can get this data from this cell. What I want is the following : if you select the forename or whatever in the row, my programm needs to know the wo_id. In the text labels on the left side, I show the selected forename and lastname.

void adminUserData::on_tableView_activated(const QModelIndex &index)
{
QString val;

val = ui->tableView->model()->data(index).toString();
QSqlQuery qry;
qry.prepare("SELECT * FROM worker WHERE wo_id='"+val+"'");
if (qry.exec())
{
   while (qry.next())
   {
      // ui->label_test->setText(qry.value(1).toString());
       val=index.column();
       ui->label_test->setText(val);
       ui->label_test2->setText(qry.value(2).toString());
   }
}
}

How to change the code in order to get the id ?

Upvotes: 3

Views: 649

Answers (2)

Yaoparfait48
Yaoparfait48

Reputation: 41

It works well for me

ui->tableView->model()->index(index.row(),0).data().toString();

Upvotes: 1

Frederik Ubben
Frederik Ubben

Reputation: 143

QModelIndex first = ui->tableView->model()->index(index.row(), 0);

If i use this as "new" index it works =)

Upvotes: 1

Related Questions