Results 1 to 11 of 11

Thread: QTableView : accessing the data

  1. #1
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QTableView : accessing the data

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView : accessing the data

    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.

  3. The following user says thank you to wysota for this useful post:

    marvaneke (22nd July 2006)

  4. #3
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: QTableView : accessing the data

    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.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView : accessing the data

    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.

  6. The following user says thank you to wysota for this useful post:

    marvaneke (23rd July 2006)

  7. #5
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView : accessing the data

    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.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView : accessing the data

    Quote Originally Posted by marvaneke
    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 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 ?


    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.

  9. The following user says thank you to wysota for this useful post:

    marvaneke (23rd July 2006)

  10. #7
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView : accessing the data

    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.

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView : accessing the data

    Argghh... Ok, that's a newbie section, I need to be more patient...

    Qt Code:
    1. //...
    2. QAbstractItemModel *md = tv->model();
    3. QModelIndex curr = tv->currentIndex();
    4.  
    5. // for QSqlQueryModel or subclasses:
    6. QSqlQueryModel *qmod = qobject_cast<QSqlQueryModel*>(md);
    7. if(!qmod) return false;
    8. QSqlRecord rec = qmod->record(curr.row()); // you have data inside the "rec" object
    9.  
    10. // for generic models:
    11. int cc = md->columnCount(curr.parent());
    12. for(int i=0;i<cc;i++){
    13. QVariant dat = md->data(curr.sibling(curr.row(), i)); // you have the data inside the 'dat' object
    14. //...
    15. }
    To copy to clipboard, switch view to plain text mode 

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

  12. The following user says thank you to wysota for this useful post:

    marvaneke (24th July 2006)

  13. #9
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView : accessing the data

    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.
    I will try that.

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

    Regards.

  14. #10
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView : accessing the data

    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.
    Last edited by marvaneke; 24th July 2006 at 22:37. Reason: This post is closed.

  15. #11
    Join Date
    Mar 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView : accessing the data

    After trying all the possibles on this page, settled for a QSqlQuery to retrieve the data.
    Using QSqlQueryModel that has been sub classed.

Similar Threads

  1. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  2. QTableView versus QTableWidget
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2006, 19:51
  3. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49
  4. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.