PDA

View Full Version : Get data from a QTableView



graciano
18th January 2010, 17:00
Hi,

When using a model/view aproach i do ...


view->edit(index);

... changes will not make efect until i press "Return" or, if i want i can quit with Esc. Right?

Now suppose i have columns A, B, and C, where A is a Primary key in the database associated with the model.
I intend to validate that key with a beforeInsert SIGNAL.

Until here it's ok but ... now for the stupid question ... how do i get the value from column A in the row i'm editing?!

Thanks

nikhilqt
19th January 2010, 07:01
Check this SIGNAL.. void dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight )

aamer4yu
19th January 2010, 07:23
QAbstractItemModel::data

graciano
19th January 2010, 16:31
Not 100% elegant ... but it works like this!

void CidadaoDialog::antesDeInserirCidadao(QSqlRecord &registo)
{
QSqlQuery query;
QString comando;
int lin, col;
QVariant conteudoDaChave;
lin = vistaCidadao->currentIndex().row();
col = 0;
conteudoDaChave = modeloCidadao->index(lin, col, QModelIndex()).data(Qt::EditRole);
comando = "SELECT * FROM Cidadao WHERE idCidadao = " + conteudoDaChave.toString()+";";
query.prepare(comando);
query.exec();
if(query.next())
{
QMessageBox msg;
msg.setText("Chave duplicada(duplicate key)!");
msg.setInformativeText(query.lastQuery());
msg.exec();
modeloCidadao->select();
}
}

What i was looking for is in line 9.

Thanks