gpt4 book ai didi

python - 扩展 QSpinBox ContextMenu

转载 作者:行者123 更新时间:2023-12-01 01:09:18 24 4
gpt4 key购买 nike

感觉我已经到处寻找但找不到答案。想知道是否有办法向 QSpinBox ContextMenu 添加额外的 QActions?最好位于操作列表的顶部。

我知道您可以替换默认菜单,如下所示,但可以弄清楚如何扩展:

import sys
from PyQt4 import QtGui, QtCore


class QSpinBox(QtGui.QSpinBox):
def __init__(self, *args, **kargs):
QtGui.QSpinBox.__init__(self, *args, **kargs)
self.resize(200, 60)

self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.__contextMenu)

def __contextMenu(self):
self._normalMenu = self.build_menu()
self._normalMenu.exec_(QtGui.QCursor.pos())

def build_menu(self):
self.menu = QtGui.QMenu(self)

self.save = QtGui.QAction("Save", self)
self.menu.addAction(self.save)

self.reset = QtGui.QAction("Reset", self)
self.menu.addAction(self.reset)

return self.menu


if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
form = QSpinBox()
form.show()
sys.exit(app.exec_())

默认菜单:

enter image description here

我想添加到顶部的选项:

enter image description here

预先感谢您的帮助,

标记

最佳答案

诀窍是获取 QMenu使用 findChild() 显示后不久与 QTimer.singleShot() :

from PyQt4 import QtCore, QtGui

class SpinBox(QtGui.QSpinBox):
def contextMenuEvent(self, event):
QtCore.QTimer.singleShot(0, self.on_timeout)
super(SpinBox, self).contextMenuEvent(event)

@QtCore.pyqtSlot()
def on_timeout(self):
menu = self.findChild(QtGui.QMenu, 'qt_edit_menu')
if menu is not None:
first_action = menu.actionAt(QtCore.QPoint())
save_action = QtGui.QAction(
"Save",
menu,
shortcut=QtGui.QKeySequence.Save,
triggered=self.on_save_triggered
)
reset_action = QtGui.QAction(
"Reset",
menu,
triggered=self.on_reset_triggered
)
menu.insertAction(first_action, save_action)
menu.insertAction(save_action, reset_action)
menu.insertSeparator(first_action)

@QtCore.pyqtSlot()
def on_save_triggered(self):
print("save")

@QtCore.pyqtSlot()
def on_reset_triggered(self):
print("reset")

if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = SpinBox()
w.resize(600, w.sizeHint().height())
w.show()
sys.exit(app.exec_())

关于python - 扩展 QSpinBox ContextMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55009488/

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