¿Cómo puedo hacer que las casillas de verificación sean editables pero no el campo Nombre en la primera columna?

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()
-1
JokerMartini 2 ene. 2017 a las 01:47

1 respuesta

La mejor respuesta

Use {your QStandardItem}.setCheckable(True), en su caso agregue model.item(0, j).setCheckable(True) después de model.setData(model.index(0, j)).

Código completo:

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

Salida:

enter image description here

1
eyllanesc 2 ene. 2017 a las 02:12