【问题标题】:PyQt5 DoubleSpinBox in QTableViewQTableView 中的 PyQt5 DoubleSpinBox
【发布时间】:2019-04-10 06:55:33
【问题描述】:

使用 PyQt5 将编辑器委托设置为 QDoubleSpinBox:

class MoneyDelegate(QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(MoneyDelegate, self).initStyleOption(option, index)
        val = float(index.data())
        option.text = '${:,.2f}'.format(val)
        option.displayAlignment = Qt.AlignVCenter | Qt.AlignRight

    def createEditor(self, QWidget, QStyleOptionViewItem, QModelIndex):
        super(MoneyDelegate, self).createEditor(QWidget, QStyleOptionViewItem, QModelIndex)
        editor = QDoubleSpinBox(self)
        editor.setMinimum(.01)
        editor.setMaximum(999999.99)
        return(editor)

在我的 QTableWidget 类中:

self.tbl_View.setItemDelegateForColumn(2, MoneyDelegate(self))

我遇到的问题是编辑器作为单独的对话框显示在屏幕的左上角。我在这里错过了什么?

【问题讨论】:

    标签: python python-3.x pyqt pyqt5


    【解决方案1】:

    不要使用QWidgetQStyleOptionViewItemQModelIndex 作为函数参数的名称,因为它们是类的名称,而您正在混淆它们。另一方面,您必须作为编辑器的父级传递给createEditor() 的第一个参数:

    def createEditor(self, parent, option, index):
        editor = QDoubleSpinBox(parent)
        editor.setMinimum(.01)
        editor.setMaximum(999999.99)
        return editor
    

    【讨论】:

    • 应该更清楚。谢谢 Eyllanesc!
    猜你喜欢
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    相关资源
    最近更新 更多