[PyQt] QDirModel instance

Phil Thompson phil at riverbankcomputing.com
Thu Jul 10 07:36:51 BST 2008


On Wed, 9 Jul 2008 22:39:27 -0700, Scott Frankel <frankel at circlesfx.com>
wrote:
> 
> Hello,
> 
> I just joined this list and am new to PyQt (and Qt).  I've scanned the  
> archives, but I'm not sure even what I'm looking for to answer my  
> question.
> 
> I'm looking over some C++ code, trying to rewrite it in Python.  (My  
> Spanish is much better than my C++!)  I'm sub-classing and extending  
> QDirModel and getting the following error:
> 
> 	TypeError: first argument of unbound method QDirModel.data() must be  
> a QDirModel instance
> 
> The C++ methods are:
> 
>      virtual QVariant data(const QModelIndex &index, int role =  
> Qt::DisplayRole) const
>      {
>          if (role == Qt::CheckStateRole && index.column() == 0) {
>              return  
> checkstates.value(fileInfo(index).absoluteFilePath(), Qt::Unchecked);
>          }
>          return QDirModel::data(index, role);
>      }
> 
> 
> My translation so far looks like this:
> 
> 
> [ ... ]
> class DirModel(QtGui.QDirModel):
> 	def data(self, index, role=QtCore.Qt.DisplayRole):
> 		if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
> 			return checkstates.value(fileInfo(index).absoluteFilePath(),  
> QtCore.Qt.Unchecked)
> 		return QtGui.QDirModel.data(index, role)
> [ ... ]
> 
> 
> Not sure how to make the first argument (index or self?) be a  
> QDirModel instance.  Ultimately I'm hoping for a directory tree with  
> check boxes.
> 
> Suggestions?

When using an unbound method (including __init__()) you must explicitly
pass self. So...

    return QtGui.QDirModel.data(self, index, role)

Phil



More information about the PyQt mailing list