Results 1 to 8 of 8

Thread: Display data from QStandardItemModel in QTreeView

  1. #1
    Join Date
    Jan 2007
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Display data from QStandardItemModel in QTreeView

    Hi, this is my first post to the forum. I have an application that queries for data on a network. Then populates a QStandardItemModel in the following fashion;

    Qt Code:
    1. QStandardItemItem * parentItem = model->invisibleRootItem();
    To copy to clipboard, switch view to plain text mode 

    I iterate over my data instantiating new child items to be added to the model.
    Qt Code:
    1. item->setData(QVariant("some data"));
    2. parentItem->setChild(row,column,item);
    To copy to clipboard, switch view to plain text mode 

    Now the data is in the model, I have gone through it and written the data back out to the console.

    Next I set the model in my treeview in this way;

    Qt Code:
    1. treeView->setModel(model);
    2. QStandardItem * parentItem = model->invisibleRootItem();
    3. QModelIndex index = parentItem->index();
    4. treeView->setRootIndex(index);
    5. QModelIndex currentIndex = model->index(0,0);
    6. treeVeiw->setCurrentIndex(currentIndex);
    7. treeView->setRootIndex(index);
    To copy to clipboard, switch view to plain text mode 

    When the application runs and I get to the page with the treeview display, I see the model column header labels but no data.

    I don't understand why I don't see the data, any assistance would be appreciated.

    thanks
    Last edited by wysota; 9th January 2007 at 15:45. Reason: missing [code] tags

  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: Display data from QStandardItemModel in QTreeView

    My guess is you don't have any rows in your model - try using QStandardItemModel::appendRow

  3. #3
    Join Date
    Jan 2007
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Display data from QStandardItemModel in QTreeView

    Quote Originally Posted by wysota View Post
    My guess is you don't have any rows in your model - try using QStandardItemModel::appendRow
    I was passing the row and column arguments in the parentItem->setChild(row,column,item) call.

    I tried your suggestion in two ways first just adding the extra call, still no data and a warning from qt about duplicate items. The display showed two faint angle lines on the left margin that should be followed by the data.

    The second try with just the appendRow() call yeilded the same are results of the original implementation, no data and one faint angle line on the left margin.

  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: Display data from QStandardItemModel in QTreeView

    Try this:
    Qt Code:
    1. model->setColumnCount(1);
    2. model->setRowCount(5);
    3. model->setData(model->index(0, 0), "some data0");
    4. model->setData(model->index(1, 0), "some data1");
    5. model->setData(model->index(2, 0), "some data2");
    6. model->setData(model->index(3, 0), "some data3");
    7. model->setData(model->index(4, 0), "some data4");
    8. QTreeView *treeView = new QTreeView(this);
    9. treeView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2007
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: Display data from QStandardItemModel in QTreeView

    I found the resolution to displaying data in my QTreeView object. What I found was that not only do you have to set the item data but you must also set the item text. In my initialization of the model after setting the item data I added the following line and now see all data in my model displayed in the treeview.

    item->setText(tr("some data"));

    My guess is that it is done this way for internationalization.

  6. #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: Display data from QStandardItemModel in QTreeView

    void QStandardItem::setData ( const QVariant & value, int role = Qt::UserRole + 1 )
    setText() is equivalent to
    Qt Code:
    1. setData("some data", Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2007
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Display data from QStandardItemModel in QTreeView

    Quote Originally Posted by wysota View Post
    setText() is equivalent to
    Qt Code:
    1. setData("some data", Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    Now thats interesting. When I populated the model I was aware of the "roles" but obviously didn't understand the implications. Thanks for the information.

  8. #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: Display data from QStandardItemModel in QTreeView

    QStandardItem (and other convenience item classes like QTreeWidgetItem) simply has convenience methods like setText, setToolTip, setFont, setIcon, etc. that are wrappers to setData with appropriate roles.

Similar Threads

  1. Modify model data in QTreeView
    By YuriyRusinov in forum Qt Programming
    Replies: 6
    Last Post: 26th October 2006, 17:28
  2. can't do a QTreeView with a QStandardItemModel
    By Nuria in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2006, 00:41
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  4. Can't change the text display of a QTreeview item.
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2006, 01:03
  5. Dynamic Data Display with OpenGL
    By showhand in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2006, 01:17

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.