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