Question

How can I make the spinner that is right-clicked have it's value changed to the minimum value of that particular QSpinBox? This should work for each spinner in this UI. So the top spinner's value would change to 1 when right-clicked and the bottom spinners value would change to 0 when that spinner is right-clicked.

#!/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()
Was it helpful?

Solution

Here's how to add a an item to the default context-menu that should do what you want:

    ...
    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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top