gpt4 book ai didi

python - 鼠标右键单击时使QSpinBox值更改为最小值 Pyside + Python

转载 作者:太空宇宙 更新时间:2023-11-04 03:51:57 24 4
gpt4 key购买 nike

如何使右键单击的微调器的值更改为该特定 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()

最佳答案

以下是如何将一个项目添加到默认的上下文菜单,该菜单应该执行您想要的操作:

    ...
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)

关于python - 鼠标右键单击时使QSpinBox值更改为最小值 Pyside + Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20914065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com