python - PySide 中的自定义委托(delegate)

标签 python qt delegates pyside

我一直在尝试让 tabeView 将其一列显示为组合框。为了做到这一点,我编写了自定义委托(delegate)的代码:

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 属性似乎不起作用。任何帮助将不胜感激。

最佳答案

我知道这已经很旧了,但你真的根本不需要处理这幅画。组合框不显示值,因为组合框当前索引可能设置为字符串而不是整数。

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

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

关于python - PySide 中的自定义委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10037529/

相关文章:

ios - ContainerViewController 委托(delegate)给 ChildViewController

ios - 数组未通过委托(delegate)从 TableView 传递到 View Controller

c# - 如何为 Action<T1,T2> 获取可选参数

python , Pandas 。从累积值转换为增量

python - 我如何在 pyqt4 的 Qt Designer 中使用 promote to?

python - 以像素为单位指定 Tkinter 文本框的尺寸

qt - 我可以用 Qt 编写一个与 Gnome 一起运行的应用程序吗?

python - 将值从解析传递到管道初始化

C++ QT libXL 错误 : "During Startup program exited with code 0xc0000135"

c++ - Qt QQuickView 视觉错误/调整大小时闪烁