右键单击Pyside + Python时,将QSpinBox值更改为最小值

时间:2014-01-03 22:43:01

标签: python pyside qspinbox

如何使右键单击的微调器将其值更改为该特定QSpinBox的最小值?这应该适用于此UI中的每个微调器。因此,右键单击时顶部微调器的值将更改为1,右键单击该微调器时,底部微调器值将更改为0。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import math
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        #ESTIMATED TOTAL RENDER TIME
        self.spinFrameCountA = QtGui.QSpinBox()
        self.spinFrameCountA.setRange(1,999999)
        self.spinFrameCountA.setValue(40)

        self.spinFrameCountB = QtGui.QSpinBox()
        self.spinFrameCountB.setRange(0,999999)
        self.spinFrameCountB.setValue(6)

        # UI LAYOUT
        grid = QtGui.QGridLayout()
        grid.setSpacing(0)
        grid.addWidget(self.spinFrameCountA, 0, 0, 1, 1)
        grid.addWidget(self.spinFrameCountB, 1, 0, 1, 1)
        self.setLayout(grid)

        self.setGeometry(800, 400, 100, 50)
        self.setWindowTitle('Render Time Calculator')
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

以下是如何将项目添加到应该执行所需操作的默认上下文菜单中:

    ...
    self.spinFrameCountA = QtGui.QSpinBox()
    self.spinFrameCountA.setRange(1,999999)
    self.spinFrameCountA.setValue(40)
    self.spinFrameCountA.installEventFilter(self)

    self.spinFrameCountB = QtGui.QSpinBox()
    self.spinFrameCountB.setRange(0,999999)
    self.spinFrameCountB.setValue(6)
    self.spinFrameCountB.installEventFilter(self)
    ...

def eventFilter(self, widget, event):
    if (event.type() == QtCore.QEvent.ContextMenu and
        isinstance(widget, QtGui.QSpinBox)):
        menu = widget.lineEdit().createStandardContextMenu()
        menu.addSeparator()
        menu.addAction('Reset Value',
                       lambda: widget.setValue(widget.minimum()))
        menu.exec_(event.globalPos())
        menu.deleteLater()
        return True
    return QtGui.QWidget.eventFilter(self, widget, event)