Would you kindly add more of your code, I have a similar questions and I found this post, I'm having problem accessing the right data.
in my main constructor, I have the following connect statement:
connect( viewAllPatients, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(rowDoubleClicked(QModelIndex)));
To copy to clipboard, switch view to plain text mode
Then, using an example I found online ... my slot is defined below, but I get error with the view widget 'viewAppPatients' (which is a QTableView)
void CentralPage
::rowDoubleClicked(const QModelIndex &QMI
) {
QVariant value
= viewAllPatients
->model
()->data
(QMI,
0);
if (value.isValid())
sRec = value.toString();
qDebug() << sRec;
}
void CentralPage::rowDoubleClicked(const QModelIndex &QMI)
{
QString sRec = "not valid column";
QVariant value = viewAllPatients->model()->data(QMI,0);
if (value.isValid())
sRec = value.toString();
qDebug() << sRec;
}
To copy to clipboard, switch view to plain text mode
So, these posts make wonder where would I define/use AbstracItemModel, because this is how I defined model in my constructor:
model->setQuery("SELECT FirstName,LastName FROM Clients");
model->setHeaderData(0,Qt::Horizontal, tr("First"));
model->setHeaderData(1,Qt::Horizontal, tr("Last Name"));
viewAllPatients->setModel(model);
viewAllPatients->setAlternatingRowColors(true);
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT FirstName,LastName FROM Clients");
model->setHeaderData(0,Qt::Horizontal, tr("First"));
model->setHeaderData(1,Qt::Horizontal, tr("Last Name"));
QTableView *viewAllPatients = new QTableView;
viewAllPatients->setModel(model);
viewAllPatients->setAlternatingRowColors(true);
To copy to clipboard, switch view to plain text mode
Bookmarks