Results 1 to 8 of 8

Thread: QTreeView and QStandardModel : add children

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView and QStandardModel : add children

    How do you expect to use them if they are not initialised?

  2. #2
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView and QStandardModel : add children

    What should I initialize them to then? I´m sorry, but I´m new to the model/view way of doing this and am just happy that it´s working at all The docs themselves are rather vague on this subject as well I find

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

    Default Re: QTreeView and QStandardModel : add children

    Quote Originally Posted by Valheru View Post
    What should I initialize them to then?
    I don't know. It is you who used them. Didn't you know what you needed them for?

    The docs themselves are rather vague on this subject as well I find
    Hmm.... Maybe you didn't look well enough...

    Quote Originally Posted by Qt4 docs
    An example usage of QStandardItemModel to create a tree:
    Qt Code:
    1. QModelIndex parent;
    2. for (int i = 0; i < 4; ++i) {
    3. parent = model->index(0, 0, parent);
    4. model->insertRows(0, 1, parent);
    5. model->insertColumns(0, 1, parent);
    6. QModelIndex index = model->index(0, 0, parent);
    7. model->setData(index, i);
    8. }
    To copy to clipboard, switch view to plain text mode 
    It creates a cascade of 4 elements. If you want to have a two level tree you should modify the code a little, so that the parent index doesn't get changed at each iteration:
    Qt Code:
    1. int parentrow = 2; // top level row number you wish to create offsprings for
    2. QStringList connectionData;
    3. connectionData << "Con1" << "Con2" << "Con3" << "Con4";
    4. QModelIndex parent = model->index(parentrow, 0);
    5. model->insertColumns(0, 1, parent); // this creates a single column for children of the item
    6. for (int i = 0; i < connectionData.size(); ++i) {
    7. model->insertRows(0, 1, parent);
    8. QModelIndex index = model->index(i, 0, parent);
    9. model->setData(index, connectionData[i]);
    10. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeView and QStandardModel : add children

    Go through and try to understand the following code:
    Qt Code:
    1. void ServerList::addItem( const QString &hostName, quint16 port, const QString& username,
    2. const QString& password, quint16 timeout, quint16 numberConnections )
    3. {
    4. // first, insert a top level row
    5. int rows = model()->rowCount();
    6. model()->insertRow( rows );
    7.  
    8. // set host name to the index in the first
    9. // column of the newly inserted row
    10. QModelIndex parent = model()->index( rows, 0 );
    11. model()->setData( parent, hostName );
    12.  
    13. //Server ns( hostName, port, username, password, timeout, numberConnections );
    14. //server.append( ns );
    15.  
    16. // add a column and enough rows for connection children
    17. // the parent is the parent we just inserted above
    18. model()->insertColumn( 0, parent ); // one column for children
    19. rows = model()->rowCount( parent );
    20. model()->insertRows( rows, numberConnections, parent ); // one row for each child
    21.  
    22. // set connection names for each children
    23. for ( int x = 0; x < numberConnections; x++ )
    24. {
    25. QModelIndex child = model()->index( x, 0, parent ); // all connections are under the same parent
    26. // which is, again, the same parent we
    27. // inserted in the first place
    28. model()->setData( child, QString("Connection #%1").arg( x + 1 ) );
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Valheru (19th September 2006)

  6. #5
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView and QStandardModel : add children

    Thanks, that works perfectly. I'm now just trying to puzzle out why

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.