how to add an action to QtCore.Qt.DefaultContextMenu on Qdoublespinbox on right cilck?(如何在右侧 cilck 上的 Qdoublespinbox 上向 QtCore.Qt.DefaultContextMenu 添加操作?)
问题描述
我使用 Qt Designer 开发了一个相当复杂的 GUI 工具.
有关该工具的更多详细信息,请参阅:
然后点击添加按钮,然后点击提升按钮.
对于另一个QDoubleSpinBox
,右键单击并选择DoubleSpinBox
选项所在的新Promote To选项.
您可以在这里找到一个示例
I have developed a fairly complex GUI tool using the Qt Designer.
For more details about the tool see: https://github.com/3fon3fonov/trifon
I have defined many QDoubleSpinBox entries and by default the Qt Designer sets their right-click menu policy to:
setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
Now I want to add few more actions to this menu, but I simply cannot understand how this works! There is nothing in the Qt Designer which will allow me to make a "CustomContextMenu". I understand that for this I may need some coding (with which I will need help, and thus I am asking for help here), but I also need to make it globally for all SpinBox-es.
Sorry for not posting the code since it is fairly large for this form. If interested, please look at the github under "gui.py". However, there and in the .ui file there is no sign of any possibility to control the contextmenu policy for these buttons. Instead I am posting an image of the tool (sorry for the bad image but PrtSc does not seem to work when the right button in clicked and the menu is displayed)
see GUI image here
As we want to add a QAction
to the default context menu we first overwrite the contextMenuEvent
event and use a QTimer
to call a function that filters the toplevels
and get the QMenu
that is displayed and there we add the QAction
:
doublespinbox.py
from PyQt5 import QtCore, QtWidgets
class DoubleSpinBox(QtWidgets.QDoubleSpinBox):
minimize_signal = QtCore.pyqtSignal()
def __init__(self, *args, **kwargs):
super(DoubleSpinBox, self).__init__(*args, **kwargs)
self.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
def contextMenuEvent(self, event):
QtCore.QTimer.singleShot(0, self.add_actions)
super(DoubleSpinBox, self).contextMenuEvent(event)
@QtCore.pyqtSlot()
def add_actions(self):
for w in QtWidgets.QApplication.topLevelWidgets():
if isinstance(w, QtWidgets.QMenu) and w.objectName() == "qt_edit_menu":
w.addSeparator()
minimize_action = w.addAction("minimize this parameter")
minimize_action.triggered.connect(self.minimize_signal)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = DoubleSpinBox()
w.show()
sys.exit(app.exec_())
To use DoubleSpinBox in Qt Designer, first place doublespinbox.py next to your .ui:
├── ..
├── rvmod_gui.ui
├── doublespinbox.py
├── ...
then you must promote the widget to do so right click on the QDoubleSpinBox and select the option "Promote to ..." by adding the following to the dialog:
Then click on the Add button and then the Promote button.
For the other QDoubleSpinBox
, right click and select the new Promote To option where the DoubleSpinBox
option is.
You can find an example here
这篇关于如何在右侧 cilck 上的 Qdoublespinbox 上向 QtCore.Qt.DefaultContextMenu 添加操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在右侧 cilck 上的 Qdoublespinbox 上向 QtCore.Qt.DefaultContextMenu 添加操作?
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01