[PyQt] Fwd: QStandardItem check state

Jamie Riotto jamie.riotto at gmail.com
Fri Jun 11 22:35:05 BST 2010


He,
Hopefully this helps:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import sys
from random import randint

class view(QListView):

   def __init__(self, parent=None):
       super(view, self).__init__(parent)

       model = QStandardItemModel()

       for n in range(10):
           item = QStandardItem('Item %s' % randint(1, 100))
           check = Qt.Checked if randint(0, 1) == 1 else Qt.Unchecked

           item.setCheckState(check)
           item.setCheckable(True)

           model.appendRow(item)

       self.setModel(model)
       self.show()

       model.itemChanged.connect(self.on_itemChanged)


   @pyqtSlot(QStandardItem)
   def on_itemChanged(self,  item):
       state = ['UNCHECKED', 'TRISTATE',  'CHECKED'][item.checkState()]
       print "Item with text '%s', is at state %s\n" % ( item.text(),  state)


if __name__ == '__main__':
   app = QApplication(sys.argv)
   widget = view()
   widget.show()


--------------------------------------------------------------

Cheers - jamie

On Fri, Jun 11, 2010 at 10:59 AM, He Jibo <hejibo at gmail.com> wrote:
> Hell,
>
> I am trying to build a selection tree, as below.  I hope to which files are
> checked.
>
> O filename1
> O filename2
> O filename3
> O filename4
> ......
>
>
> I find the following codes fit my need well. But I do not know how to get
> the check state signal. I hope, each time an item is checked or unchecked, I
> get a state change signal, and find the lists of checked items.
> Can someone help me out? Thanks.
>
> http://stackoverflow.com/questions/846684/a-listview-of-checkboxes-in-pyqt
>
>
>
> from PyQt4.QtCore import *
>
>
> from PyQt4.QtGui import *
>
>
> import sys
> from random import randint
>
>
> app = QApplication(sys.argv)
>
>
>
> model = QStandardItemModel()
>
> for n in range(10):
>
>
>     item = QStandardItem('Item %s' % randint(1, 100))
>
>
>     check = Qt.Checked if randint(0, 1) == 1 else Qt.Unchecked
>
>
>     item.setCheckState(check)
>     item.setCheckable(True)
>
>
>     model.appendRow(item)
>
>
> view = QListView()
>
>
> view.setModel(model)
>
> view.show()
>
>
> app.exec_()
>
>
>
> ---------------------------
> He Jibo
> Department of Psychology,
> Beckman Institute for Advanced Science and Technology
> University of Illinois, Urbana Champaign,
> 603 East Daniel St.,
> Champaign, IL 61820
> website: www.hejibo.info
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>


More information about the PyQt mailing list