【问题标题】:QWidgetItem override by setDefaultFactory in PyQt4 Example coloreditorfactoryQWidgetItem 在 PyQt4 Example coloreditorfactory 中被 setDefaultFactory 覆盖
【发布时间】:2014-03-02 14:21:26
【问题描述】:

有人试过他们的 PyQt4 示例 coloreditorfactory 吗?

通常,我可以使用setFlags() 或setData() 方法创建可编辑的QWidgetItem,但在setDefaultFactory 方法之后它不起作用。我看到一些文档说所有新的或现有的委托都将被 setDefaultFactory 覆盖。

有什么方法可以让 QWidgetItem 再次可编辑?或恢复setDefaultFactory?谢谢~ (QTextEdit或QLineEdit等除外)

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

【问题讨论】:

    标签: python pyqt4 factory qitemdelegate


    【解决方案1】:

    顾名思义,default factory 将为所有新的和现有的代表提供项目编辑器。因此,如果您希望工厂与特定委托一起工作,请仅在该委托上设置工厂:

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

    【讨论】:

    • 你是对的,它有效。设置特定委托后,可以避免覆盖“默认工厂”。谢谢。
    • 只是好奇,如果我只想在此表中将一个单元格设置为ColorListItemEditorCreator 怎么办?
    • @juseedom。有用于为列或行设置委托的内置方法,但不适用于特定项目。要执行后者,您必须创建一个自定义委托,该委托保留任何不符合您的包含条件的项目的默认行为。
    猜你喜欢
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 2012-08-07
    相关资源
    最近更新 更多