[PyQt] Why am I not able to check the checkboxes

vijay swaminathan swavijay at gmail.com
Mon Mar 21 05:21:56 GMT 2011


Hi All,

I tried creating a treeview with checkboxes but I'm unable to select the
checkboxes.

on the flag method I had mentioned it as ItemisuserCheckable but still could
not get it working...

am I missing something here to enable the selection of checkboxes.

A snippet of the code is:

import sys

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




class StbTreeView(QAbstractListModel):
    def __init__(self, args, parent=None):
        super(StbTreeView, self).__init__(parent)

        self.args = args
        print self.args


    def rowCount(self, parent):
        return len(self.args)

    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return QString("Select STB's")

    def flags(self, index):
        row = index.row()
        if row:
            return  Qt.ItemIsUserCheckable | Qt.ItemIsEnabled |
Qt.ItemIsEditable | Qt.ItemIsSelectable

    def data(self, index, role=Qt.DisplayRole):

        if role == Qt.DisplayRole:
            row = index.row()
            return self.args[row]

        if role == Qt.CheckStateRole:

            row = index.row()
            return QVariant(Qt.Unchecked)


    def setData(self, index, value, role):


        if role == Qt.CheckStateRole:
            if value == Qt.Checked:
                row = index.row()
                selected_stb = self.args[row]
                print 'selected_stb is %s' % selected_stb
                print 'Value is %s' % value

self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),index, index)
                return True
                #return QVariant(Qt.Checked)




def main():
    myapp = QApplication(sys.argv)

    data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6', 'STB7', 'STB8']

    model = StbTreeView(data)
    tree_view = QTreeView()
    tree_view.show()
    tree_view.setModel(model)


    myapp.exec_()


if __name__ == '__main__':
    main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110321/80e511d3/attachment.html>


More information about the PyQt mailing list