pyside复选框可编辑

pyside checkboxes editable

如何使复选框可编辑但第一列中的名称字段不可编辑?

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()

使用 {your QStandardItem}.setCheckable(True),在您的情况下,在 model.setData(model.index(0, j)).

之后添加 model.item(0, j).setCheckable(True)

完整代码:

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)

            for i in range(1, len(headers)):
                model.setData(model.index(0, i), QtCore.Qt.Unchecked, role=QtCore.Qt.CheckStateRole)
                model.item(0, i).setCheckable(True)


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Browser()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

输出: