[PyQt] qTreeWidgetItemIterator : how it works

Baz Walter bazwal at ftml.net
Sat May 31 15:46:12 BST 2014


On 31/05/14 08:19, singhai.nish wrote:
> Hello Baz,
>
>                    As of now this code prints blank. Nothing in the result.
> Blank.
>
>   self.light_write  <PyQt4.QtGui.QTreeWidget object at 0x10e76510>
>   iterator  <PyQt4.QtGui.QTreeWidgetItemIterator object at 0x10a16d70>
>
> Under this layer, what should i look at to know that the problem is here (
> literally speaking ).
>
> Does the iterator require other parameters to be set before.

Probably not. Here's a simple working example that you may find helpful:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
     def __init__(self):
         QtGui.QWidget.__init__(self)
         self.tree = QtGui.QTreeWidget(self)
         parent = QtGui.QTreeWidgetItem(self.tree, ['rigname1'])
         QtGui.QTreeWidgetItem(parent, ['light01'])
         parent = QtGui.QTreeWidgetItem(parent, ['light02'])
         QtGui.QTreeWidgetItem(parent, ['object02'])
         self.tree.expandAll()
         self.button = QtGui.QPushButton('Print', self)
         self.button.clicked.connect(self.handleButton)
         layout = QtGui.QVBoxLayout(self)
         layout.addWidget(self.tree)
         layout.addWidget(self.button)

     def handleButton(self):
         iterator = QtGui.QTreeWidgetItemIterator(self.tree)
         while iterator.value():
             item = iterator.value()
             print item.text(0)
             iterator += 1

if __name__ == '__main__':

     import sys
     app = QtGui.QApplication(sys.argv)
     window = Window()
     window.resize(300, 200)
     window.show()
     sys.exit(app.exec_())

-- 
Regards
Baz Walter


More information about the PyQt mailing list