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.
ServerList
::ServerList( QWidget *p
){
parent = p;
model->setHeaderData( 0, Qt::Horizontal, "Server List" );
setModel( model );
///Set up the actions for the right-click context menu
connectToServer
= new QAction(tr
("Connect"),
this);
separator->setSeparator(true);
newServer
= new QAction(tr
("New Server"),
this);
editServer
= new QAction(tr
("Edit Server"),
this);
deleteServer
= new QAction(tr
("Delete Server"),
this);
///Set up the menu
menu->addAction(connectToServer);
menu->addAction(separator);
menu->addAction(newServer);
menu->addAction(editServer);
menu->addAction(deleteServer);
///Connect the actions
connect( connectToServer, SIGNAL( triggered() ), this, SLOT( connectToServerSlot() ) );
connect( newServer, SIGNAL( triggered() ), this, SLOT( newServerSlot() ) );
connect( editServer, SIGNAL( triggered() ), this, SLOT( editServerSlot() ) );
connect( deleteServer, SIGNAL( triggered() ), this, SLOT( deleteServerSlot() ) );
}
ServerList::ServerList( QWidget *p)
{
parent = p;
model = new QStandardItemModel( 0, 1 );
model->setHeaderData( 0, Qt::Horizontal, "Server List" );
setModel( model );
setSelectionMode( QAbstractItemView::SingleSelection );
///Set up the actions for the right-click context menu
connectToServer = new QAction(tr("Connect"), this);
separator = new QAction(this);
separator->setSeparator(true);
newServer = new QAction(tr("New Server"), this);
editServer = new QAction(tr("Edit Server"), this);
deleteServer = new QAction(tr("Delete Server"), this);
///Set up the menu
menu = new QMenu();
menu->addAction(connectToServer);
menu->addAction(separator);
menu->addAction(newServer);
menu->addAction(editServer);
menu->addAction(deleteServer);
///Connect the actions
connect( connectToServer, SIGNAL( triggered() ), this, SLOT( connectToServerSlot() ) );
connect( newServer, SIGNAL( triggered() ), this, SLOT( newServerSlot() ) );
connect( editServer, SIGNAL( triggered() ), this, SLOT( editServerSlot() ) );
connect( deleteServer, SIGNAL( triggered() ), this, SLOT( deleteServerSlot() ) );
}
To copy to clipboard, switch view to plain text mode
void ServerList
::addItem( const QString &hostName, quint16 port,
const QString
& username,
const QString& password, quint16 timeout, quint16 numberConnections )
{
int rows = model->rowCount( parent );
model->insertRows( rows, 1, parent );
model->setData( index, hostName );
Server ns( hostName, port, username, password, timeout, numberConnections );
server.append( ns );
for( int x = 0; x < numberConnections; x++ ){
QModelIndex parentServer
= model
->index
( rows,
0, index
);
int serverRow = model->rowCount( parentServer );
model->insertRows( serverRow, 1, parentServer );
QModelIndex connectionIndex
= model
->index
( serverRow,
0, parentServer
);
model->setData( connectionIndex, s );
}
}
void ServerList::addItem( const QString &hostName, quint16 port, const QString& username,
const QString& password, quint16 timeout, quint16 numberConnections )
{
QModelIndex parent = model->index( 0, 0, parent );
int rows = model->rowCount( parent );
model->insertRows( rows, 1, parent );
QModelIndex index = model->index( rows, 0, parent );
model->setData( index, hostName );
Server ns( hostName, port, username, password, timeout, numberConnections );
server.append( ns );
for( int x = 0; x < numberConnections; x++ ){
QModelIndex parentServer = model->index( rows, 0, index );
int serverRow = model->rowCount( parentServer );
model->insertRows( serverRow, 1, parentServer );
QModelIndex connectionIndex = model->index( serverRow, 0, parentServer );
QString s("Connection #");
s += QString::number( x + 1 );
model->setData( connectionIndex, s );
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks