Results 1 to 4 of 4

Thread: Reading items in a QStandardItemModel

  1. #1
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Reading items in a QStandardItemModel

    ok i may be being thick here but i have a problem.
    i subclassed QStandardItemModel for my app, and have set up a model with 4 columns. when i read a list of items from a file it adds the rows in arraying the items in the correct order. that is File, Title, Artist, Album. now when i double click on the treeview it calls the following code

    Qt Code:
    1. void myTunes::treeDclick(QModelIndex index)
    2. {
    3. QString str(model->fileForIndex(index));
    4. Phonon::MediaSource src(str);
    5. mediaObject->stop();
    6. mediaObject->setCurrentSource(src);
    7. mediaObject->play();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QString mediaLibrary::fileForIndex(QModelIndex index)
    2. {
    3. int i;
    4. i = index.row();
    5. return QString(item(i,0)->text());
    6. }
    To copy to clipboard, switch view to plain text mode 

    when compiled the programme runs fine untill the double click event when the code
    Qt Code:
    1. return QString(item(i,0)->text());
    To copy to clipboard, switch view to plain text mode 
    creates an error and the debugger halts the programme execution. so my question is why??? and how do i get around it???

    as there are no errors shown i cant post them. any help would be very much appreciated.

    ta janorcutt

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading items in a QStandardItemModel

    first of all you can get your string directly from QModelIndex:
    Qt Code:
    1. index.data(Qt::DisplayRole).toString();
    To copy to clipboard, switch view to plain text mode 
    if it is in the column 0. If not then:
    Qt Code:
    1. index = index.model()->index(index.row(), 0, index.parent());
    To copy to clipboard, switch view to plain text mode 
    should work I think.

    Second of all: is your model a tree structure? If yes then you forgot about parents. item(i, 0) gives you top level item in row i and column 0. So when you have some child nodes it will work incorrectly when clicking child node, because it will try to get text from top level item in the same row as your clicked child item.

    Third of all: item(i, 0)->text() already return QString so no need to construct QString copy which would be copied and destructed in the next line, so it should be:
    Qt Code:
    1. return item(i, 0)->text();
    To copy to clipboard, switch view to plain text mode 

    Fourth of all: it is good to check first if pointer returned by item(i. 0) is valid. For example in your "parents case" item at row i and column 0 may not exist so item() will return 0. Using ->text() on null pointer gives you what? That's right: Segmentation fault! :]
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: Reading items in a QStandardItemModel

    aahhh, thanks it works now. my model is actually a table but i suppose you still need to tell qt exactly where to look in relation to the index passed.

    like so

    Qt Code:
    1. QString mediaLibrary::fileForIndex(QModelIndex index)
    2. {
    3. int i;
    4. i = index.row();
    5. QModelIndex ind(index.model()->index(i,0,index.parent()));
    6.  
    7. return ind.data(Qt::DisplayRole).toString();
    8. }
    To copy to clipboard, switch view to plain text mode 

    also i've hidden the 1st column '0' in the tree so i needed to reference it in relation to the index passed by the event...

    thanks again

    janorcutt

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading items in a QStandardItemModel

    And when you are not sure that the given index will be always valid then remember to check is with:
    Qt Code:
    1. if (!index.isValid()) {
    2. // not valid! do sth, e.g. return;
    3. }
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    janorcutt (17th February 2010)

Similar Threads

  1. Getting Child Items from QStandardItemModel
    By nikhilqt in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2013, 15:10
  2. QStandardItemModel Help
    By frenk_castle in forum Newbie
    Replies: 1
    Last Post: 16th January 2010, 17:54
  3. Replies: 3
    Last Post: 19th November 2009, 14:10
  4. Replies: 2
    Last Post: 30th July 2009, 10:20
  5. Replies: 2
    Last Post: 24th August 2008, 14:42

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
  •  
Qt is a trademark of The Qt Company.