如何在右侧cilck的Qdoublespinbox上的QtCore.Qt.DefaultContextMenu中添加动作? [英] how to add an action to QtCore.Qt.DefaultContextMenu on Qdoublespinbox on right cilck?

查看:214
本文介绍了如何在右侧cilck的Qdoublespinbox上的QtCore.Qt.DefaultContextMenu中添加动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Qt Designer开发了一个相当复杂的GUI工具.

I have developed a fairly complex GUI tool using the Qt Designer.

有关该工具的更多详细信息,请参见: https://github.com/3fon3fonov/trifon

For more details about the tool see: https://github.com/3fon3fonov/trifon

我定义了许多QDoubleSpinBox条目,默认情况下,Qt Designer将其右键菜单策略设置为:

I have defined many QDoubleSpinBox entries and by default the Qt Designer sets their right-click menu policy to:

setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)

现在,我想在此菜单中添加一些其他操作,但是我根本无法理解其工作原理! Qt设计器中没有任何东西可以让我创建"CustomContextMenu".我了解为此,我可能需要一些编码(需要帮助,因此我在这里寻求帮助),但是我还需要针对所有SpinBox-es使其全局化.

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.

很抱歉,由于此表单的代码太大,因此无法发布.如果有兴趣,请查看"gui.py"下的github.但是,在.ui文件中,没有任何迹象表明可以控制这些按钮的上下文菜单策略. 相反,我发布了该工具的图片(很抱歉,图片质量不佳,但是当单击鼠标右键并显示菜单时,PrtSc似乎不起作用)

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)

在此处查看GUI图像

推荐答案

当我们要将QAction添加到默认上下文菜单时,我们首先覆盖contextMenuEvent事件并使用QTimer调用一个函数来过滤toplevels并获取显示的QMenu,然后在其中添加QAction:

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_())


要在Qt Designer中使用DoubleSpinBox,请首先在.ui旁边放置 doublespinbox.py :

├── ..
├── rvmod_gui.ui
├── doublespinbox.py   
├── ...

然后,您必须对窗口小部件进行升级,以右键单击QDoubleSpinBox并通过在对话框中添加以下内容来选择升级为..."选项:

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.

对于其他QDoubleSpinBox,右键单击并选择DoubleSpinBox选项所在的新的Promote To选项.

For the other QDoubleSpinBox, right click and select the new Promote To option where the DoubleSpinBox option is.

您可以在此处

这篇关于如何在右侧cilck的Qdoublespinbox上的QtCore.Qt.DefaultContextMenu中添加动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆