Results 1 to 8 of 8

Thread: QTreeView and QStandardModel : add children

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

    Default QTreeView and QStandardModel : add children

    I have a QTreeView subclassed, and it has a QStandardModel so as to be able to provide headers. I add items to the model from a database, namely servers and the connections to them. There are multiple connections to every server, with a default of 4. The code I have adds the connections under the servers, with no indentation and no arrow or + sign to collapse the servers and make the connections invisible. I assume that I'm adding the connections wrong and they are not being added as children.

    Qt Code:
    1. ServerList::ServerList( QWidget *p)
    2. {
    3. parent = p;
    4. model = new QStandardItemModel( 0, 1 );
    5. model->setHeaderData( 0, Qt::Horizontal, "Server List" );
    6. setModel( model );
    7. setSelectionMode( QAbstractItemView::SingleSelection );
    8. ///Set up the actions for the right-click context menu
    9. connectToServer = new QAction(tr("Connect"), this);
    10. separator = new QAction(this);
    11. separator->setSeparator(true);
    12. newServer = new QAction(tr("New Server"), this);
    13. editServer = new QAction(tr("Edit Server"), this);
    14. deleteServer = new QAction(tr("Delete Server"), this);
    15. ///Set up the menu
    16. menu = new QMenu();
    17. menu->addAction(connectToServer);
    18. menu->addAction(separator);
    19. menu->addAction(newServer);
    20. menu->addAction(editServer);
    21. menu->addAction(deleteServer);
    22. ///Connect the actions
    23. connect( connectToServer, SIGNAL( triggered() ), this, SLOT( connectToServerSlot() ) );
    24. connect( newServer, SIGNAL( triggered() ), this, SLOT( newServerSlot() ) );
    25. connect( editServer, SIGNAL( triggered() ), this, SLOT( editServerSlot() ) );
    26. connect( deleteServer, SIGNAL( triggered() ), this, SLOT( deleteServerSlot() ) );
    27. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ServerList::addItem( const QString &hostName, quint16 port, const QString& username,
    2. const QString& password, quint16 timeout, quint16 numberConnections )
    3. {
    4. QModelIndex parent = model->index( 0, 0, parent );
    5. int rows = model->rowCount( parent );
    6. model->insertRows( rows, 1, parent );
    7. QModelIndex index = model->index( rows, 0, parent );
    8. model->setData( index, hostName );
    9. Server ns( hostName, port, username, password, timeout, numberConnections );
    10. server.append( ns );
    11. for( int x = 0; x < numberConnections; x++ ){
    12. QModelIndex parentServer = model->index( rows, 0, index );
    13. int serverRow = model->rowCount( parentServer );
    14. model->insertRows( serverRow, 1, parentServer );
    15. QModelIndex connectionIndex = model->index( serverRow, 0, parentServer );
    16. QString s("Connection #");
    17. s += QString::number( x + 1 );
    18. model->setData( connectionIndex, s );
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 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

    What are 'parent' and 'parentServer' initialised to?

  3. #3
    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

    Erm, they're not :/ What you see is what you get, in this case

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 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?

  5. #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

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 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 

  7. #7
    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

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

    Valheru (19th September 2006)

  9. #8
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.