简体   繁体   English

在Qt Designer中回调按钮?

[英]Callback for button in Qt Designer?

I just started using QtCreator tonight, and it seems it puts all of the interface stuff inside of the ui file. 我今晚刚开始使用QtCreator,它似乎把所有接口内容放在ui文件中。 I followed a tutorial to create a resource for my icons, then I added them to a menu bar at the top. 我按照教程为我的图标创建了一个资源,然后我将它们添加到顶部的菜单栏中。

I need to make a connection when one of them is clicked though, and cannot figure out how to make a callback for it. 我需要在点击其中一个时建立连接,并且无法弄清楚如何为它进行回调。
Am I going to have to completely create them through code or is there some way to add a callback for them (rather than just making them interact with other objects). 我是否必须通过代码完全创建它们,或者是否有某种方法为它们添加回调(而不仅仅是让它们与其他对象交互)。

Menu bar items are action objects . 菜单栏项是操作对象 To do something when they are clicked, you need to catch the triggered() signal from the action. 要在单击它们时执行某些操作,您需要捕获来自操作的triggered() 信号 Read more about signals and slots here . 在此处阅读有关信号和插槽的更多信

To do this, you need to declare a new slot in your MainWindow class. 为此,您需要在MainWindow类中声明一个新 Qt also supports doing this automatically, without the need to connect anything, but I prefer doing it myself. Qt还支持自动执行此操作,无需连接任何内容,但我更喜欢自己动手。 If you're not interested, just skip this part. 如果您不感兴趣,请跳过此部分。

First, we declare a new slot in your window class: 首先,我们在您的窗口类中声明一个新槽:

private slots:
  void clickMenuButton();

Then, in your constructor, you need to connect the triggered signal to your new slot: 然后,在构造函数中,您需要将触发信号连接到新插槽:

connect(ui.actionObject, SIGNAL(triggered()), this, SLOT(clickMenuButton()));

The first argument is the object that holds the signal we'll listen to (your menu button). 第一个参数是保存我们将要收听的信号的对象(您的菜单按钮)。 The second is the name of the signal. 第二个是信号的名称。 The third is the object that holds the receiving slot (in this case, our window). 第三个是保存接收槽的对象(在本例中是我们的窗口)。 The fourth is the slot. 第四个是插槽。

And just like that, clickMenuButton() will be called whenever the action is clicked. 就这样,只要单击操作,就会调用clickMenuButton()

As I said before, Qt can also automatically connect signals to slots . 正如我之前所说,Qt还可以自动将信号连接到插槽 The disadvantage here seems to be that you can't change the slot's name, but you don't need to connect it either. 这里的缺点似乎是您无法更改插槽的名称,但您也不需要连接它。

Qt Creator supports creation of slots for widgets: in the case of your menu action, you should go to the form designer, and you should see a list of actions in your form (if you don't, find the Action Editor ). Qt Creator支持为小部件创建插槽:在菜单操作的情况下,您应该转到表单设计器,您应该在表单中看到一系列操作(如果没有,请找到操作编辑器 )。 Right click the action you want, and push Go to slot... . 右键单击所需的操作,然后按“ 转到插槽...” There, double click triggered() . 在那里,双击triggered()

Qt Creator will then open the new slot in your code editor, and you can do whatever you want to here! 然后,Qt Creator将在您的代码编辑器中打开新的插槽,您可以在此处执行任何操作!

To do that you'll need to add a QAction , add it to the menu, associate an icon with it and then create a callback for it. 为此,您需要添加QAction ,将其添加到菜单中,将图标与其关联,然后为其创建回调。 I'm using the VS Integration so I don't know the details of how to do it in Creator but it should be possible without creating stuff in code. 我正在使用VS集成,因此我不知道如何在Creator中执行此操作的详细信息,但应该可以在不创建代码的情况下实现。

There should be somewhere an actions editor. 应该有某个动作编辑器。 from there you add an action, then right-click it or something to add an icon to it, then drag it do the menu and then possibly double click it to create a slot for it. 从那里你添加一个动作,然后右键单击它或其他东西,为它添加一个图标,然后拖动它做菜单,然后可能双击它为它创建一个插槽。 This is how it works in the VS Integration atleast. 这是它至少在VS集成中的工作方式。

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

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