[Interest] [Qt-4.8] Subclassing QAbstractItemModel

Christian Quast christian.quast at cquast-it.de
Sat Feb 9 14:11:15 CET 2013


Dear All,

I currently get to know the Qt Model/View classes. So I went on and created a 
subclass of QAbstractItemModel to provide the data for the different views in 
Qt. As my structure is tree like, I followed the SimpleTreeModel example that 
comes with Qt.

At first, I had some problems implementing the required functions correctly 
(data, index, parent, rowCount, columnCount). So I added some output at the 
top of each function. Finally I got everything to work and the data is being 
displayed correctly.

However, one thing I noticed is that whenever the view changes focus or it is 
resized the complete model seems to redrawn. After looking at my code checking 
why this would happen I decided to put the same output at the top of every 
function in the SimpleTreeModel:
    it has the same result.

Reducing the number lines that get parsed in the SimpleTreeModel to two 
produces this 'stacktrace' for the above mentioned case:
4x TM::columnCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
8x TM::headerData 
   TM::rowCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::columnCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::rowCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::columnCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::rowCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::index QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::rowCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::columnCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::rowCount QModelIndex(0,0,0x19e3ad0,TreeModel(0x7fff3f3aa590) )  
   TM::columnCount QModelIndex(0,0,0x19e3ad0,TreeModel(0x7fff3f3aa590) )  
   TM::columnCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::parent QModelIndex(0,0,0x19e3ad0,TreeModel(0x7fff3f3aa590) )  
   TM::index QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::rowCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::columnCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
8x TM::data QModelIndex(0,0,0x19e3ad0,TreeModel(0x7fff3f3aa590) )  8 
   TM::index QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::rowCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
   TM::columnCount QModelIndex(-1,-1,0x0,QObject(0x0) ) 
8x TM::data QModelIndex(0,1,0x19e3ad0,TreeModel(0x7fff3f3aa590) )  13 
8x TM::headerData 


For a rather large model this seems quite some work. So my question is, is 
this behaviour to be expected?

Thanks for your help
  Christian



More information about the Interest mailing list