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.
Code:
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() ) );
}
Code:
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 );
}
}
Re: QTreeView and QStandardModel : add children
What are 'parent' and 'parentServer' initialised to?
Re: QTreeView and QStandardModel : add children
Erm, they're not :/ What you see is what you get, in this case :(
Re: QTreeView and QStandardModel : add children
How do you expect to use them if they are not initialised?
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 :p The docs themselves are rather vague on this subject as well I find :(
Re: QTreeView and QStandardModel : add children
Quote:
Originally Posted by
Valheru
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?
Quote:
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:
Code:
for (int i = 0; i < 4; ++i) {
parent = model->index(0, 0, parent);
model->insertRows(0, 1, parent);
model->insertColumns(0, 1, parent);
model->setData(index, i);
}
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:
Code:
int parentrow = 2; // top level row number you wish to create offsprings for
connectionData << "Con1" << "Con2" << "Con3" << "Con4";
model->insertColumns(0, 1, parent); // this creates a single column for children of the item
for (int i = 0; i < connectionData.size(); ++i) {
model->insertRows(0, 1, parent);
model->setData(index, connectionData[i]);
}
Re: QTreeView and QStandardModel : add children
Go through and try to understand the following code:
Code:
void ServerList
::addItem( const QString &hostName, quint16 port,
const QString
& username,
const QString& password, quint16 timeout, quint16 numberConnections )
{
// first, insert a top level row
int rows = model()->rowCount();
model()->insertRow( rows );
// set host name to the index in the first
// column of the newly inserted row
model()->setData( parent, hostName );
//Server ns( hostName, port, username, password, timeout, numberConnections );
//server.append( ns );
// add a column and enough rows for connection children
// the parent is the parent we just inserted above
model()->insertColumn( 0, parent ); // one column for children
rows = model()->rowCount( parent );
model()->insertRows( rows, numberConnections, parent ); // one row for each child
// set connection names for each children
for ( int x = 0; x < numberConnections; x++ )
{
QModelIndex child
= model
()->index
( x,
0, parent
);
// all connections are under the same parent // which is, again, the same parent we
// inserted in the first place
model
()->setData
( child,
QString("Connection #%1").
arg( x
+ 1 ) );
}
}
Re: QTreeView and QStandardModel : add children
Thanks, that works perfectly. I'm now just trying to puzzle out why :p