[Qt-interest] QTreeView + Custom Model

Oliver Ney oliver at dryder.de
Sat Aug 29 19:05:44 CEST 2009


Hi,

I am currently trying to get a QTreeView with a custom Model working.
My problem is, that the QTreeView displays all top level elements and
also shows the [+]/[-] box if there are child elements, but it does not
expand the elements. Which means that [+] changes into [-] and the tree
stays collapsed.

By adding debugging messages to my code I found out that the QTreeView
calls the index() function of my model with row = -1 when it's asked to
expand the tree.

I've already read all parts of the Qt Model/View-Framework pages in the
documentation for this and also used the TreeView example in the docs as
a reference.

The data I'm displaying in the TreeView comes from a MySQL database but
I can't use QSqlTableModel for it.

I've written some testing code for model, which I think should show that
it works fine.

The data it should display can be found here:
http://www.dryder.de/oliver/modeldata.png (Screenshot from phpMyAdmin)

My testing code is:

for(int i = 0; i < m_products_model->rowCount(); i++)
{
	QModelIndex index = m_products_model->index(i, 0);
	if(!index.isValid())
	{
		qDebug("Invalid index");
		continue;
	}
			
	qDebug(index.data(Qt::DisplayRole).toString().toLatin1());

	for(int j = 0; j < m_products_model->rowCount(index); j++)
	{
		QModelIndex child = m_products_model->index(i, 0, index);
		if(!child.isValid())
		{
			qDebug("Invalid index");
			continue;
		}

	
qDebug(QString('\t').append(child.data(Qt::DisplayRole).toString()).toLatin1());
	}
}

The output is:

Testprodukt
	Testvariant
Föhnen

This is how I understood the models work. So I don't get it why the
TreeView asks for row -1. And doesn't display and child elements.

Any help is highly appreciated. Thanks for taking the time to read this.

Best regards,

Oliver



More information about the Qt-interest-old mailing list