PDA

View Full Version : QTableView : accessing the data



marvaneke
22nd July 2006, 14:51
Hi,

I have a mysql table "person" with the columns id and name.
I have this code :
QSqlTableModel *model = new QSqlTableModel;
model->setTable("person");
model->setEditStrategy(QSqlTableModel::OnRowChange);
model->select();
PersonTableView->setModel(model);

When I select a row of "PersonTableView", I want to access the data of each column. For example, I want to receive the id=1, and name="schmit".

Can you give me the code for accessing the data from the "PersonTableView", and not from the model.

I have already read the documentation, but I need the example code.

Regards.

wysota
22nd July 2006, 18:09
Hmm... The view is for viewing data... it doesn't hold any data itself, only presents it to the user. There is no way of accessing the data in model-view without using the model. The view can give you model indexes associated with selected items (QSelectionModel can handle that), but whole data access has to go through the model. That's the whole idea in model-view concept.

marvaneke
22nd July 2006, 21:37
Thanks Wysota.

You are right.

But could you send me the code for :
1) retrieve the model from the QTableView. I suppose that it is :
PersonTableView->selectRow ( 2 ); // 2 is an example
QItemSelectionModel *ism = PersonTableView->selectionModel();
I suppose that, after that, ism contain the record 2.

2) retrieve the ID from the current record showing in the QTableView. This code does not work :
int i = ism->select(ism->currentIndex(), QItemSelectionModel::Current).toInt();

Can you help me ?

Regards.

wysota
23rd July 2006, 00:28
I told you, you can't access data through the view.

You can use QAbstractItemView::currentIndex() to fetch the current item and then QAbstractItemModel::data() to access its data. If you need the whole row, repeat the process modifying the column in the index retrieved with currentIndex.

marvaneke
23rd July 2006, 09:41
Wysota, thanks for the answer.

I will try that.

I am suprise in front of the diffuclty to build something somethat very simple : "Accessing the data of a view".
Searching the internet, I do not find example code that fill my requirement.

Have I wrong searching ?

The internet address, and his external link :
http://en.wikipedia.org/wiki/Qt_%28toolkit%29
was not helpful enough.

Do you know some uselful address ?

If I found the solution, have I to post a simple example, on qtcentre.org, on trolltech.com, on somewhere else ?

This is also a call to the trolltech team, please post MORE example code on your site doc.trolltech.com.

Regards.

wysota
23rd July 2006, 11:33
I am suprise in front of the diffuclty to build something somethat very simple : "Accessing the data of a view".
Searching the internet, I do not find example code that fill my requirement.
It's not a difficulty. In the model-view pattern (http://en.wikipedia.org/wiki/Model-view-controller) you simply don't access the data through the view, because that functionality is in the model. The view is only for displaying data.


Have I wrong searching ?
I think you simply misunderstood the concept of a view.


Do you know some uselful address ?

QtCentre (http://www.qtcentre.org)
QtCentre wiki (http://wiki.qtcentre.org)
QtCentre links (http://www.qtcentre.org/index.php?option=com_weblinks)



If I found the solution, have I to post a simple example, on qtcentre.org, on trolltech.com, on somewhere else ?
It won't be a solution but rather a hack around the model-view concept. You can use the QTableWidget widget instead of QTableView and you'll be able to access the data through the widget without an external model.

If you do find a nice solution to your problem, you can publish it here on the forum or in the wiki.


This is also a call to the trolltech team, please post MORE example code on your site doc.trolltech.com.
Why should Trolltech post example code at their site? Qt docs contain many examples (and this is a proper place to put them). TT website is a company site not a Qt site.

marvaneke
24th July 2006, 18:31
Well, now, I think I have understood the MVC.
I think that QTableView assotiated with a model is a good solution for me.
But, I do not find the way for going from one row selected in the QTableView to the model which contains the related record.
I can go from QTableView to the model with this instruction :
QItemSelectionModel *ism = PersonTableView->selectionModel();
but with ism, I do not find the instruction to access the data of the record.

Can someone give me the code ?

Regards.

wysota
24th July 2006, 20:41
Argghh... :) Ok, that's a newbie section, I need to be more patient... :cool:


QTableView *tv;
//...
QAbstractItemModel *md = tv->model();
QModelIndex curr = tv->currentIndex();

// for QSqlQueryModel or subclasses:
QSqlQueryModel *qmod = qobject_cast<QSqlQueryModel*>(md);
if(!qmod) return false;
QSqlRecord rec = qmod->record(curr.row()); // you have data inside the "rec" object

// for generic models:
int cc = md->columnCount(curr.parent());
for(int i=0;i<cc;i++){
QVariant dat = md->data(curr.sibling(curr.row(), i)); // you have the data inside the 'dat' object
//...
}

As you see in both cases the data is accessed through the model.

marvaneke
24th July 2006, 20:59
You do not have to apologize for trying to freely help me (under the GPL license). ;-)
Yes, I am simply a newbie. But I think, I am not alone. ;-)
Without your help, it was impossible for me to find this code. :cool:
I will try that.

Thank you very well, for the quality of your answer, and for your great knowledge. ;)

Regards.

marvaneke
24th July 2006, 21:42
Hi wysota,

I've got it.
For me, your :"// for generic models:" is better, for accessing the ID of the person.

Still the code is not optimized, it is working. ;)

It is inside the person.cpp file.

void Person::on_OkButton_clicked()
{
qWarning( "on_OkButton_clicked : begin" );

QAbstractItemModel *aim = PersonTableView->model();
QModelIndex mi = PersonTableView->currentIndex();
qDebug() << "Person::on_OkButton_clicked() : mi.row()=" << mi.row() ;
int i = aim->data(mi.sibling(mi.row(), 0)).toInt();
qDebug() << "Person::on_OkButton_clicked() : i=" << i ;

PersonIdSet(aim->data(mi.sibling(mi.row(), 0)).toInt());
PersonDetail *mw = new PersonDetail;
mw->show();

qWarning( "on_OkButton_clicked : end" );
}

So on the PersonDetail, I can manage only one record.

Once again, thank you very much.

Regards.

libmgg
30th March 2012, 11:31
After trying all the possibles on this page, settled for a QSqlQuery to retrieve the data.
Using QSqlQueryModel that has been sub classed.