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

Zoltan Szalai defaultdict at gmail.com
Sat Mar 26 09:42:37 GMT 2011


hi,

you always return Qt.Unchecked for Qt.CheckStateRole in the data method ...
try the attached code.

bests
Zoltan




On 2011.03.21. 6:21, vijay swaminathan wrote:
> 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()
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110326/2d636074/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cb.py
Type: application/x-python
Size: 2036 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110326/2d636074/attachment-0001.bin>


More information about the PyQt mailing list