简体   繁体   English

Qt Designer中的自定义菜单操作

[英]Custom menu actions in Qt Designer

I'm currently trying to use Qt Designer to build a GUI and I would like to customize slots of my menu actions. 我目前正在尝试使用Qt Designer来构建GUI,并且想自定义菜单操作的插槽。

Eg: I'd like the user to press a menu action and it'd show a widget if it is hidden or hide it if it is already visible. 例如:我希望用户按下菜单操作,如果它是隐藏的,它将显示一个小部件,如果已经可见,则将其隐藏。

Basically, what I want to do is execute some code of mine and not the default actions such as show() or hide(). 基本上,我想执行的是执行我的一些代码,而不是执行诸如show()或hide()之类的默认操作。

So I'm wondering if I should create a subclass of QMenuBar, add custom slots to it, then create a plugin to use it inside Qt Designer or if I should create a subclass for QMenu or QAction ? 因此,我想知道是否应该创建QMenuBar的子类,向其添加自定义插槽,然后创建一个插件在Qt Designer中使用它,还是应该为QMenu或QAction创建子类? Or maybe it isn't the right way to do that ? 也许这不是正确的方法?

I'm working under Visual Studio and I'm only using Qt Designer, not Qt creator. 我在Visual Studio下工作,仅使用Qt Designer,而不使用Qt Creator。

I'm new to GUI and Qt programming and I'm a bit lost here. 我是GUI和Qt编程的新手,在这里我迷路了。

Thanks in advance :) 提前致谢 :)

You have basically 2 options: 您基本上有2个选择:

  1. Implement the custom logic in the Mainwindow sublcass. 在Mainwindow子目录中实现自定义逻辑。
    For this, you simply add the slots required for your handling in the class, and make them available in Qt Designer. 为此,您只需在类中添加处理所需的插槽,并使它们在Qt Designer中可用。 You can do this: 你可以这样做:
    either in the Signal/Slot editor and click "Modify" and then click on the + Symbol. 无论是在信号/槽的编辑器,然后单击“修改”,然后单击+符号。 By this you make new slots available in QtDesigner; 这样,您就可以在QtDesigner中使用新的插槽了。
    or when your slot is called on_(senderName)_(signalName) , Qt autowiring will automatically connect the signals, and you don't have to do this in code or desinger. 当您的插槽称为on_(senderName)_(signalName) ,Qt自动装配将自动连接信号,您无需在代码或desinger中执行此操作。

  2. Create a QMenuBar subclass and implement the custom logic there. 创建一个QMenuBar子类并在那里实现自定义逻辑。
    Your case tell Qt Designer to select your specific subclass as replacement for the default QMenuBar by right-clicking on it, and select "Promote to...". 您的案例告诉Qt Designer通过右键单击它来选择特定的子类来替代默认的QMenuBar,然后选择“升级为...”。 In the new dialog, you can specify your custom class that will be used as replacement in actual code, but in design time a QMenuBar is used. 在新对话框中,您可以指定您的自定义类,该类将在实际代码中用作替换类,但在设计时将使用QMenuBar。 With this mehtod, you don't have to write a separate plugin to make your class available in Qt Designer. 使用此方法,您无需编写单独的插件即可在Qt Designer中使用您的类。

Note that with the second option, your custom logic will only be called when the actions are triggered through the menu bar, not by shortcuts or tool buttons 请注意,使用第二个选项时,仅当通过菜单栏而不是快捷方式或工具按钮触发操作时,才会调用自定义逻辑

Create a slot in your class: 在您的班级中创建一个广告位:

onMenuActionTriggered()

Use the connect() to react on action's signal: 使用connect()对动作的信号做出反应:

connect(ui.myAction, SIGNAL(triggered()), this, SLOT(onMenuActionTriggered()));

In your slot you can do whatever you want. 在您的广告位中,您可以做任何您想做的事。

Another solution (not my favourite one, but possible) is to use the auto-connect functionality, which means, by declaring a slot 'on_myAction_triggered()' (where myAction is the name of your QAction) you don't need to use the connect() since it is automatically connected by Qt 另一个解决方案(不是我最喜欢的解决方案,但可行)是使用自动连接功能,这意味着,通过声明插槽“ on_myAction_triggered()”(其中myAction是您的QAction的名称),您不需要使用connect()因为它是由Qt自动连接的

The menu bar is automatically added to any new form derived from QMainWindow (the default when creating a gui application, but you can create new main windows using file->new file or project... and selecting Qt->Qt Designer Form Class ). 菜单栏会自动添加到QMainWindow派生的任何新表单(创建gui应用程序时的默认表单,但您可以使用file-> new file或project ...并选择Qt-> Qt Designer Form Class来创建新的主窗口) 。

To add options to it you simply click in the area labeled "Type here" and write your option text. 要向其中添加选项,只需单击标有“在此处键入”的区域,然后输入选项文本。 When you do so an action will appear in a list in the lower part of Qt Designer. 当您这样做时,一个动作将出现在Qt Designer下部的列表中。 Right click on that action and select "go to slot". 右键单击该动作,然后选择“转到广告位”。 It will pop up a dialog with "triggered()" already selected for you. 它将弹出一个对话框,其中已经为您选择了“ triggered()”。 Simply click "Ok" and Qt Creator will take care of all the details and transport you to the body of the slot function. 只需单击“确定”,Qt Creator就会处理所有细节并将您带到插槽功能的正文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM