Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to select Checkbox inside TreeView
    text
    copied!<p>I tried creating a treeview with checkboxes but I'm unable to select the checkboxes.</p> <p>on the flag method I had mentioned it as ItemisuserCheckable but still could not get it working...</p> <p>am I missing something here to enable the selection of checkboxes.</p> <p>A snippet of the code is:</p> <pre><code>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() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload