Results 1 to 5 of 5

Thread: QT4 - get content of a QAbstractItemModel

  1. #1
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default QT4 - get content of a QAbstractItemModel

    I have this piece of code that I found on the forum :
    Qt Code:
    1. QListView *lv; // I use a QListView to view my QAbstractItemModel
    2. ...
    3. QItemSelectionModel *sel = lv->selectionModel();
    4. bool third = sel->isRowSelected(3, lv->rootIndex()); // check if row "3" is selected
    5. QModelIndexList list = sel->selectedIndexes();
    6. foreach(QModelIndex index, list){
    7. qDebug("Row %d selected", index.row());
    8. }
    To copy to clipboard, switch view to plain text mode 

    How can adapt it to read the content of the model row by row without foreach ?
    In fact I need the contents of rows 3,4, up to rowCount().

    Could I do foreach and have :
    Qt Code:
    1. foreach(QModelIndex index, list){
    2. if (index.row() > 2) qDebug("Row %d selected", index.row());
    3. }
    To copy to clipboard, switch view to plain text mode 
    But how do I set the index to contain all rows (selected or not) ?
    And how can I get the actual value for each index ?
    Last edited by incapacitant; 17th March 2006 at 18:43.

  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: QT4 - get content of a QAbstractItemModel


  3. #3
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT4 - get content of a QAbstractItemModel

    I can't get it right :
    Qt Code:
    1. for (int row = 3; row < qrecap_model->rowCount(); row++)
    2. {
    3. QModelIndex idx = (qrecap_model->index(row, 0));
    4. QString s = qrecap_model->data(idx); <- this does not compile
    5. }
    To copy to clipboard, switch view to plain text mode 
    236 C:\Qt\test\sms\menu.cpp no matching function for call to `QAbstractItemModel::data(QModelIndex**)'

    A bit of help ?

  4. #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: QT4 - get content of a QAbstractItemModel

    Surely you have to convert QVariant, which gets returned by the model to QString by using QVariant::toString(). But I'm wondering where does this "QModelIndex**" come from. Could you show a larger portion of your code?

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

    incapacitant (18th March 2006)

  6. #5
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT4 - get content of a QAbstractItemModel

    Qt Code:
    1. if ( qrecap_model->rowCount() > 3 )
    2. {
    3. for (int row = 3; row < qrecap_model->rowCount(); row++)
    4. {
    5. QModelIndex idx = (qrecap_model->index(row, 0));
    6. s = qrecap_model->data(idx).toString();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    adding toString() has made it compile ?

    Anyway big thanks

Similar Threads

  1. How to make QAbstractItemModel 's data checkable
    By nifei in forum Qt Programming
    Replies: 12
    Last Post: 1st April 2013, 20:52
  2. Creating a QAbstractItemModel for QTreeView
    By hbill in forum Qt Programming
    Replies: 13
    Last Post: 14th August 2008, 17:01
  3. Replies: 5
    Last Post: 20th April 2008, 21:30
  4. QAbstractItemModel to different views
    By ganeshshenoy in forum Qt Programming
    Replies: 3
    Last Post: 19th February 2008, 14:43
  5. Printing the content
    By veda in forum Qt Programming
    Replies: 7
    Last Post: 27th December 2007, 13:03

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.