PDA

View Full Version : getting current Id from tableView



mkarakaplan
7th October 2007, 20:37
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.

wysota
7th October 2007, 21:16
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.

mkarakaplan
7th October 2007, 21:37
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

wysota
7th October 2007, 22:14
If you have a "clicked" signal that emits the index that was clicked, there is no point in retrieving the index manually again.