[PyKDE] Typecasting

Per Gummedal p.g at figu.no
Sat Nov 11 10:00:57 GMT 2000


11.11.2000, 01:38, Mark Kimsal wrote:

> Okay, i've been stuck all day on this problem.

> I have a class that subclasses QListViewItem, FTPDirItem.  I populate a
> QListView with a few instances of FTPDirItem.  When I connect the signal
> from the containing QListView it requires a pointer to an object of type
> QListViewItem.  When I try to access object variables inside my SLOT
> function I get AttributeErrors.  type(myobj) reveals that the program
> thinks the object is a QListViewItem at that point.  I can access
> user-defined object methods of FTPDirItem, but whenever I try to access
> variables I created in the subclasses' __init__() method I get an
> error.  So my question is how can I typecast this object back into an
> FTPDirItem inside my SLOT callback function?

This works for me:

import string, sys
from qt import *

class List(QListView):
      def __init__(self, *args):
          apply(QListView.__init__,(self,) + args)
          self.addColumn("A")
          self.addColumn("B")
          self.itemList = []
          self.data = (['1','2'], ['11','12'],['2','3'])
          self.connect(self, SIGNAL("rightButtonClicked(QListViewItem *, const QPoint &, int)"), \
                                    self.rbClicked)
          self.addItems()
                
      def addItems(self):
          for row in self.data:
              item = TestItem(self)
              self.itemList.append(item)
              for i in range(len(row)):
                  item.setText(i, row[i])
        
      def rbClicked(self, item, point, col):
          print item.subvar, item.text(0)

class TestItem(QListViewItem):
      def __init__(self, parent):
          QListViewItem.__init__(self, parent)
          self.subvar = 'Test'
                                
a = QApplication(sys.argv)
mw = List()
a.setMainWidget(mw)
mw.show()
a.exec_loop()


Per






More information about the PyQt mailing list