getting current Id from tableView
I took this example from Qt4 examples.
model= new QSqlRelationalTableModel(this);
model->setTable("person");
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last Name"));
model->select();
tableView->setModel(model);
I added
QObject::connect(tableView, SIGNAL(clicked(QModelIndex)), getItem, SLOT(click()));
Now i want to take person.id from this model in the current selection. I need an example. Thanks.
Re: getting current Id from tableView
Make your slot accept a const QModelIndex& parameter and use QModelIndex::data() to fetch the data from the 0th column. This will be your id.
Re: getting current Id from tableView
QModelIndex retorno = tableView->currentIndex();
int row = retorno.row();
QVariant ok = model->data(model->index(row,0) );
qDebug("ID:%i ", ok.toInt() );
I made like this.
Thanks
Re: getting current Id from tableView
If you have a "clicked" signal that emits the index that was clicked, there is no point in retrieving the index manually again.