Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PySide中的自定义委托_Python_Qt_Delegates_Pyside - Fatal编程技术网

Python PySide中的自定义委托

Python PySide中的自定义委托,python,qt,delegates,pyside,Python,Qt,Delegates,Pyside,我一直在尝试让tabeView将其中一列显示为组合框。为此,我为自定义委托编写了代码: class comboBoxDelegate(QStyledItemDelegate): def __init__(self, model, parent=None): super(comboBoxDelegate, self).__init__(parent) self.parent= parent self.model= model def createEditor(self,

我一直在尝试让tabeView将其中一列显示为组合框。为此,我为自定义委托编写了代码:

class comboBoxDelegate(QStyledItemDelegate):

def __init__(self, model, parent=None):
    super(comboBoxDelegate, self).__init__(parent)
    self.parent= parent
    self.model= model

def createEditor(self, parent, option, index):

    if not index.isValid():
        return False

    self.currentIndex=index  

    self.comboBox = QComboBox(parent)
    self.comboBox.setModel(self.model)
    value = index.data(Qt.DisplayRole)
    self.comboBox.setCurrentIndex(value)

    return self.comboBox

def setEditorData(self, editor, index):
    value = index.data(Qt.DisplayRole)
    editor.setCurrentIndex(value)

def setModelData(self, editor, model, index):

    if not index.isValid():
        return False

    index.model().setData(index, editor.currentIndex(), Qt.EditRole)

def paint(self, painter, option, index):
    currentIndex= index.data(Qt.DisplayRole)

    opt= QStyleOptionComboBox()
    opt.rect= option.rect
    currentComboIndex= self.model.createIndex(currentIndex,0)
    opt.currentText= self.model.data(currentComboIndex, Qt.DisplayRole)

    QApplication.style().drawComplexControl(QStyle.CC_ComboBox, opt, painter)

问题是,当我尝试它时,组合框一开始不会显示任何文本(只有在单击它之后)。currentText属性似乎不起作用。任何帮助都将不胜感激。

我认为您应该调用父类paint()方法。加:

QStyledItemDelegate.paint(self, painter, option, index)

在类中的paint方法结束时,在调用
drawComplexControl

后,可以重写
qstyledItemDeleteGate.displayText()
方法,使代理显示文本,而无需重新实现paint()。差不多

class comboBoxDelegate(QStyledItemDelegate):
    ...
    def displayText(self, value, locale=None):
        return get_appropriate_text_representation_for_value(value)

我知道这幅画很古老,但你真的不需要处理这幅画。combobox不显示值,因为combobox当前索引可能被设置为字符串而不是int

class ComboBoxDelegate(QtGui.QStyledItemDelegate):
    """ComboBox view inside of a Table. It only shows the ComboBox when it is
       being edited.
    """
    def __init__(self, model, itemlist=None):
        super().__init__(model)
        self.model = model
        self.itemlist = None
    # end Constructor

    def createEditor(self, parent, option, index):
        """Create the ComboBox editor view."""
        if self.itemlist is None:
            self.itemlist = self.model.getItemList(index)

        editor = QtGui.QComboBox(parent)
        editor.addItems(self.itemlist)
        editor.setCurrentIndex(0)
        editor.installEventFilter(self)
        return editor
    # end createEditor

    def setEditorData(self, editor, index):
        """Set the ComboBox's current index."""
        value = index.data(QtCore.Qt.DisplayRole)
        i = editor.findText(value)
        if i == -1:
            i = 0
        editor.setCurrentIndex(i)
    # end setEditorData

    def setModelData(self, editor, model, index):
        """Set the table's model's data when finished editing."""
        value = editor.currentText()
        model.setData(index, value)
    # end setModelData
# end class ComboBoxDelegate

此委托仅在编辑项目时显示组合框,否则将显示普通文本项目委托。

是否检查了
self.model.data(currentComboIndex,Qt.DisplayRole)
是否实际返回了有效字符串?谢谢,是的,它确实返回了stringCalling
QStyledItemDelegate.paint(self、painter、option、index)
完全撤消在此之前完成的任何绘制。如果您总是将此作为
paint
的最后一条语句,那么您最好不要覆盖
paint