Domanda

has anyone tried they PyQt4 example coloreditorfactory?

Normally, I can create editable QWidgetItem with the setFlags() or setData() method, but it does not work after setDefaultFactory method. I saw some documentation that said all new or existing delegates will be overriden by setDefaultFactory.

Is there any method to make QWidgetItem editable again? or revert setDefaultFactory? thanks~ (Except QTextEdit or QLineEdit, etc.)

import sip
sip.setapi('QVariant', 2)

from PyQt4 import QtCore, QtGui

class ColorListEditor(QtGui.QComboBox):
    def __init__(self, widget=None):
        super(ColorListEditor, self).__init__(widget)

        self.populateList()

    def getColor(self):
        color = self.itemData(self.currentIndex(), QtCore.Qt.DecorationRole)
        return color

    def setColor(self, color):
        self.setCurrentIndex(self.findData(color, QtCore.Qt.DecorationRole))

    color = QtCore.pyqtProperty(QtGui.QColor, getColor, setColor, user=True)

    def populateList(self):
        for i, colorName in enumerate(QtGui.QColor.colorNames()):
            color = QtGui.QColor(colorName)
            self.insertItem(i, colorName)
            self.setItemData(i, color, QtCore.Qt.DecorationRole)


class ColorListItemEditorCreator(QtGui.QItemEditorCreatorBase):
    def createWidget(self, parent):
        return ColorListEditor(parent)  

class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        factory = QtGui.QItemEditorFactory()
        factory.registerEditor(QtCore.QVariant.Color,
                ColorListItemEditorCreator())
        QtGui.QItemEditorFactory.setDefaultFactory(factory)
        self.createGUI()

    def createGUI(self):
        tableData = [
            ("Alice", QtGui.QColor('aliceblue')),
            ("Neptun", QtGui.QColor('aquamarine')),
            ("Ferdinand", QtGui.QColor('springgreen'))
        ]

        table = QtGui.QTableWidget(3, 3)
        table.setHorizontalHeaderLabels(["Name", "Hair Color", "Editable"])
        table.verticalHeader().setVisible(False)
        table.resize(150, 50)

        for i, (name, color) in enumerate(tableData):
            nameItem = QtGui.QTableWidgetItem(name)
            colorItem = QtGui.QTableWidgetItem()
            colorItem.setData(QtCore.Qt.DisplayRole, color)            
            table.setItem(i, 0, nameItem)
            table.setItem(i, 1, colorItem)

            #add codes here
            editableItem = QtGui.QTableWidgetItem("xxxx")
            editableItem.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled)
            editableItem.setData(QtCore.Qt.EditRole, "xxxx")
            table.setItem(i, 2, editableItem)

        table.resizeColumnToContents(0)
        table.horizontalHeader().setStretchLastSection(True)

        layout = QtGui.QGridLayout()
        layout.addWidget(table, 0, 0)
        self.setLayout(layout)

        self.setWindowTitle("Color Editor Factory")


if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
È stato utile?

Soluzione

As its name implies, the default factory will provide the item editor for all new and existing delegates. So if you want a factory to work with a specific delegate, set the factory on that delegate only:

class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.createGUI()

    def createGUI(self):
        ...    
        self.delegate = QtGui.QStyledItemDelegate(self)
        factory = QtGui.QItemEditorFactory()
        factory.registerEditor(QtCore.QVariant.Color,
                ColorListItemEditorCreator())
        self.delegate.setItemEditorFactory(factory)
        table.setItemDelegateForColumn(1, self.delegate)
        ...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top