[Qt-interest] Problem with subclassing of the QAbstractTableModel

Vladimir Barbarosh vladimir.barbarosh at gmail.com
Fri Nov 5 16:56:19 CET 2010


I use the following code to test your class

	#include <QtCore>
	#include <QtGui>

	//! Model for viewing correspondencies.
	class CorrespondenceModel
	...

	int
	main(int argc, char **argv)
	{
		QApplication app(argc, argv);

		CorrespondenceModel model;
		model.insertRows(0, 5);

		QTableView view;
		view.setModel(&model);
		view.show();

		return app.exec();
	}

It seems to work well.  I just change you data() method:

	QVariant
	data( const QModelIndex & index , int role ) const
	{
		...
		if (role == Qt::DisplayRole || role == Qt::EditRole) {
			...
		}
		...
	}

Then I try to use QTreeView as my view:

	#include <QtCore>
	#include <QtGui>

	//! Model for viewing correspondencies.
	class CorrespondenceModel
	...

	int
	main(int argc, char **argv)
	{
		QApplication app(argc, argv);

		CorrespondenceModel model;
		model.insertRows(0, 5);

		QTreeView view;
		view.setModel(&model);
		view.show();

		return app.exec();
	}

If click on [+] sign the situation is as you describe it

> QTreeView with my model and my application crashed with cycleing calls
> of the rowCount() and columnCount() methods

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);
	}

P.S.  I think that QAbstractTableModel must do that.  Is this bug or by
      intention?

P.P.S.  Why you do not use QTableView as your view?



More information about the Qt-interest-old mailing list