View Full Version : How to read data from column 0 when I've a QModelIndex
TomASS
4th August 2009, 10:20
Hi,
I have const QModelIndex &index and I can read the data by example:
index.model()->data(index, Qt::DisplayRole).toString()
but how can I read the data from column = 0 (or any other column) and row = index.row() from this same model?
numbat
4th August 2009, 10:26
QAbstractItemModel * model = index.model();
model->data(model->index(row, col), Qt::DisplayRole);
TomASS
4th August 2009, 10:32
QAbstractItemModel * model = index.model();
model->data(model->index(row, col), Qt::DisplayRole);
Tanks, but I get:
error: invalid conversion from `const QAbstractItemModel*' to `QAbstractItemModel*'
in line:
QAbstractItemModel * model = index.model();
:/
numbat
4th August 2009, 10:39
Try:
const QAbstractItemModel * model = index.model();
model->data(model->index(row, col), Qt::DisplayRole);
windsword
6th August 2009, 14:31
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)));
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)
{
QString sRec = "not valid column";
QVariant value = viewAllPatients->model()->data(QMI,0);
if (value.isValid())
sRec = value.toString();
qDebug() << sRec;
}
So, these posts make wonder where would I define/use AbstracItemModel, because this is how I defined model in my constructor:
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);
windsword
6th August 2009, 14:33
typo, I meant:
I get error with the view widget 'viewAllPatients' (which is a QTableView)
windsword
6th August 2009, 17:18
Update: I fixed the issue and found why I was getting errors.
In my header file under private, I defined the QTableView:
QTableView *viewAllPatients;
So my Slot callback reads:
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;
}
And now, I don't have the error message:
error: ‘viewAllPatients’ was not declared in this scope
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.