Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you need to hold somewhere current item state (checked\unchecked) and change it once setdata() method is called. Your items are always unchecked because you're always returning <code>QVariant(Qt.Unchecked)</code> for them in the data() method. </p> <p>I've changed a bit your code, see it would work for you:</p> <pre><code>import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class TestItem(): def __init__(self, name, checked): self.checked = checked self.name = name class StbTreeView(QAbstractListModel): def __init__(self, args, parent=None): super(StbTreeView, self).__init__(parent) self.args = [] for item_name in args: self.args.append(TestItem(item_name, False)) for item in self.args: print item.name 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): return Qt.ItemIsUserCheckable | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled def data(self, index, role=Qt.DisplayRole): if role == Qt.DisplayRole: row = index.row() print self.args[row].name return self.args[row].name if role == Qt.CheckStateRole: row = index.row() print self.args[row].checked if self.args[row].checked == False: return QVariant(Qt.Unchecked) else: return QVariant(Qt.Checked) def setData(self, index, value, role): if role == Qt.CheckStateRole: row = index.row() self.args[row].checked = not self.args[row].checked return True 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> <p>hope this helps, regards</p>
 

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