[Qt-interest] Problem with subclassing of the QAbstractTableModel

Scott Aron Bloom Scott.Bloom at onshorecs.com
Fri Nov 5 18:33:54 CET 2010


I have been using the model test in debug mode on all my custom (derived
from QAbstractItemModel) models..

Ill usually do the following in the constructor

{
#ifdef _DEBUG
   new modelTest( this );
#endif
}

Scott

-----Original Message-----
From: Vladimir Barbarosh [mailto:vladimir.barbarosh at gmail.com] 
Sent: Friday, November 05, 2010 10:30 AM
To: Scott Aron Bloom
Cc: Igor Mironchik; qt-interest at trolltech.com
Subject: Re: [Qt-interest] Problem with subclassing of the
QAbstractTableModel

> Here is the last copy I had...
>
> Scott

Thanks.

The problems that I reveal (and fix) with the ModelTest class is:

1.  modeltest.cpp line 105

	Qt::ItemFlags flags = model->flags(QModelIndex());
	Q_ASSERT(flags == Qt::ItemIsDropEnabled || flags == 0);

    Fix:

	Qt::ItemFlags
	flags(const QModelIndex &index) const
	{
		...
		if(!index.isValid())
			return 0;
		...
	}

2.  modeltest.cpp line 247

	// Common error test #2, make sure that a second level index has
	// a parent that is the first level index.
	if (model->rowCount(topIndex) > 0) {
		QModelIndex childIndex = model->index(0, 0, topIndex);
		Q_ASSERT(model->parent(childIndex) == topIndex);
	}

    Fix:

	int
	rowCount(const QModelIndex &parent = QModelIndex()) const
	{
		if (parent.isValid())
			return 0;
		...
	}

    Just to make things right:

	int
	columnCount(const QModelIndex &parent = QModelIndex()) const
	{
		if (parent.isValid())
			return 0;
		...
	}

Wow.  This tool is really helpful.

Hmm.  But the "solution" that I post previously also works.  Here it is:

> Just adding the following lines to your class solves the problem.
>
>	QModelIndex
>	index (int row, int column, const QModelIndex &parent =
QModelIndex()) const
>	{
>		if (parent.isValid())
>			return QModelIndex();
>		return QAbstractTableModel::index(row, column, parent);
>	}

Though it is little incorrect because it make QTreeView think that the
model have
child items.  (Why else QTreeView displays [+] marks?)




More information about the Qt-interest-old mailing list