3

Is there a way to determine the width of the arrow buttons in the qspinbox? I'm trying to overwrite the context menu event, and i only want my custom event to take place if the user right-clicks over the arrow button, otherwise i want the normal context menu to appear.

Right now I'm just hardcoding a value of 20 which is not ideal.

import sys
import os
from PySide import QtGui, QtCore

class MySpinner(QtGui.QSpinBox):
    def __init__(self, parent=None):
        super(MySpinner, self).__init__(parent)
        self.setAccelerated(False)
        self.setRange(-1000,1000)
        self.setSingleStep(1)
        self.setValue(300)

    def contextMenuEvent(self, event):
        if event.pos().x() > self.rect().right()-20:
            self.setValue(50)
            self.selectAll()
        else:
            super(self.__class__, self).contextMenuEvent(event)


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.resize(300, 200)

        grid = QtGui.QVBoxLayout()
        grid.addWidget(MySpinner())
        content = QtGui.QWidget()
        content.setLayout(grid)
        self.setCentralWidget(content)



def main():
    app = QtGui.QApplication(sys.argv)
    ex = MainWindow()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
JokerMartini
  • 5,674
  • 9
  • 83
  • 193

1 Answers1

1

Instead of obtaining the width it is only necessary to obtain the SubControl to know if it was pressed in one of the arrows buttons:

def contextMenuEvent(self, event):
    opt = QtGui.QStyleOptionSpinBox()
    self.initStyleOption(opt)
    opt.subControls = QtGui.QStyle.SC_All
    hoverControl = self.style().hitTestComplexControl(QtGui.QStyle.CC_SpinBox, opt, event.pos(), self)
    if hoverControl == QtGui.QStyle.SC_SpinBoxUp:
        print("up")
    elif hoverControl == QtGui.QStyle.SC_SpinBoxDown:
        print("down")
    else:
        super(self.__class__, self).contextMenuEvent(event)

If you want to get the QRect of each subcontrol you should use

# up
rect_up = self.style().subControlRect(QtGui.QStyle.CC_SpinBox, opt, QtGui.QStyle.SC_SpinBoxUp, self)
# down
rect_down = self.style().subControlRect(QtGui.QStyle.CC_SpinBox, opt, QtGui.QStyle.SC_SpinBoxDown, self)

Another option:

def contextMenuEvent(self, event):
    opt = QtGui.QStyleOptionSpinBox()
    self.initStyleOption(opt)
    r = QtCore.QRect()
    for sc in (QtGui.QStyle.SC_SpinBoxUp, QtGui.QStyle.SC_SpinBoxDown):
        r= r.united(self.style().subControlRect(QtGui.QStyle.CC_SpinBox, opt, sc, self))
    if r.contains(event.pos()):
        print("arrow")
    else:
        super(self.__class__, self).contextMenuEvent(event)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241