[Qt-interest] Problem with subclassing of the QAbstractTableModel

Vladimir Barbarosh vladimir.barbarosh at gmail.com
Fri Nov 5 18:29:44 CET 2010


> 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