-1

I've felt into a problem: my task is getting from SQL-database some data ex (SELECT id, name, last_name FROM users);

Then i want to display this data in QTableView:

QSqlQueryModel *model = database->fetchAll(SELECT id, name, last_name FROM users);

QTableView *tableView = new QTableView();
tableView->setModel(model);
tableView->hideRow(0); // hide id
const QModelIndex selectedRowModel = tableView->selectionModel()->selectedRows()[0]; // i will get 'name', because 'id' is hidden

How can i get id of the current selected row?

Joe Dea
  • 29
  • 2
  • 7

1 Answers1

1

Well, I had the same problem and I solved it using this:

QSqlQueryModel *model = database->fetchAll(SELECT id, name, last_name FROM users);

QTableView *tableView = new QTableView();
tableView->setModel(model);
tableView->hideRow(0); 

tableView->showRow(0); //use showRow() everytime you need the value. 

const QModelIndex selectedRowModel = tableView->selectionModel()->selectedRows()[0];

tableView->hideRow(0);

The show/hide action is so fast that you can't see it happening.

I don't think is an elegant way to manage the problem, but at least I can retrieve the value from that.

Piontk
  • 93
  • 1
  • 6