How can i make the checkboxes editable but not the Name field in the first column?
import sys
from PySide import QtGui, QtCore
class Browser(QtGui.QDialog):
def __init__(self, parent=None):
super(Browser, self).__init__(parent)
self.initUI()
def initUI(self):
self.resize(400, 300)
self.setWindowTitle('Assets')
self.uiItems = QtGui.QTreeView()
self.uiItems.setAlternatingRowColors(True)
self.uiItems.setSortingEnabled(True)
self.uiItems.sortByColumn(0, QtCore.Qt.AscendingOrder)
self.uiItems.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.uiItems.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
self.uiItems.setRootIsDecorated(False) # remove empty space on left
self._model = QtGui.QStandardItemModel()
self.uiItems.setModel(self._model)
grid = QtGui.QGridLayout()
grid.setContentsMargins(0, 0, 0, 0)
grid.addWidget(self.uiItems, 0, 0)
self.setLayout(grid)
self.setLayout(grid)
self.uiItems.doubleClicked.connect(self.doubleClickedItem)
self.show()
self.create_model()
def doubleClickedItem(self, item):
name = item.data(role=QtCore.Qt.DisplayRole)
print name
def create_model(self):
model = self.uiItems.model()
items = [
'Cookie dough',
'Hummus',
'Spaghetti',
'Dal makhani',
'Chocolate whipped cream'
]
headers = ['name', 'old', 'new', 'on sale']
model.setHorizontalHeaderLabels(headers)
for item in items:
# model.appendRow(QtGui.QStandardItem(item))
model.insertRow(0)
# Append object
model.setData(model.index(0, 0), item)
model.setData(model.index(0, 1), QtCore.Qt.Unchecked, role = QtCore.Qt.CheckStateRole)
model.setData(model.index(0, 2), QtCore.Qt.Unchecked, role = QtCore.Qt.CheckStateRole)
model.setData(model.index(0, 3), QtCore.Qt.Unchecked, role = QtCore.Qt.CheckStateRole)
item = model.itemFromIndex(model.index(0,0))
# item.setCheckable(True)
def main():
app = QtGui.QApplication(sys.argv)
ex = Browser()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Use
{your QStandardItem}.setCheckable(True)
, in your case addmodel.item(0, j).setCheckable(True)
after ofmodel.setData(model.index(0, j))
.complete code:
Output: