PDA

View Full Version : Problem with QModelIndex



Kesy
30th October 2008, 07:43
Hi everybody, I'm writing a program with QT 4.4
I'm writing a program to read email from a SQLite database, I used a QSqlQueryModel and a QTableView, when I select a row of the table it returns a QModelIndex

connect(myTable, SIGNAL(clicked(QModelIndex)),myTextBrowser, SLOT(dispayMail(QModelIndex)));
I want that instead a QModelIndex it returns the email id, or else, how i get the email id from the QModelIndex?

Here is the code: (just a part of it, if you need more I will post it)


QSqlQuery query("SELECT email.id,addresses.name,email.subject,email.date FROM email,addresses WHERE addresses.id=email.'from'");
model = new QSqlQueryModel;
model->setQuery(query);
model->removeColumn(0); // don't show the ID
model->setHeaderData(0, Qt::Horizontal, tr("Da"));
model->setHeaderData(1, Qt::Horizontal, tr("Soggetto"));
model->setHeaderData(2, Qt::Horizontal, tr("Data"));

this->setShowGrid(FALSE);
this->verticalHeader()->hide();
this->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
this->setSelectionMode(QAbstractItemView::ContiguousSele ction);
this->setSelectionBehavior(QAbstractItemView::SelectRows );
this->setSelectionMode(QAbstractItemView::ExtendedSelect ion);
this->setModel(model);
this->show();

Thanks

Ps: Sorry for my english, but I'm swiss :)

spirit
30th October 2008, 07:52
QString email = index.data(Qt::EditRole).toString();


PS. please, use tags code.

Kesy
30th October 2008, 08:14
QString email = index.data(Qt::EditRole).toString();


PS. please, use tags code.

Thanks a lot for the quick answer.
If I use your code I get the content of the cell I clicked, but I want get the Id (from the database) of the row I clicked
this is the table

Da | Soggetto | Data

and this is the query


QSqlQuery query("SELECT email.id,addresses.name,email.subject,email.date FROM email,addresses WHERE addresses.id=email.'from'");
I just don't display the id in the QTableView

Thanks

spirit
30th October 2008, 08:20
then try this


...
if (!index.isValid())
return;

QModelIndex idx;
if (!index.column())
idx = index;
else
idx = index.model()->index(0, index.row());//0 -- id field

int id = index.data(Qt::EditRole).toInt();
...

or you also can determinate id column by name and then pass it in method index.
it's just an example/

spirit
30th October 2008, 08:23
or you can use this method


QSqlRecord QSqlQueryModel::record ( int row ) const




if (!index.isValid())
return;

int id = qobject_cast<QSqlTableModel *>(index.model())->record(index.row()).value("email.id").toInt();

Kesy
30th October 2008, 08:41
Thanks a lot man, you are a genius :D
I used this
...
if (!index.isValid())
return;

QModelIndex idx;
if (!index.column())
idx = index;
else
idx = index.model()->index(0, index.row());//0 -- id field

int id = index.data(Qt::EditRole).toInt();
...

and it worked fine.

spirit
30th October 2008, 08:44
Thanks a lot man, you are a genius :D
I used this
...
if (!index.isValid())
return;

QModelIndex idx;
if (!index.column())
idx = index;
else
idx = index.model()->index(0, index.row());//0 -- id field

int id = index.data(Qt::EditRole).toInt();
...

and it worked fine.

you are wellcome. but I suggest you to use code from my last post, it is more clear to use (for this task), but in general case this one more preferred. :)