Button callback in Qt Designer?

I just started using QtCreator today, and it seems like it puts the entire interface in a ui file. I followed the tutorial to create a resource for my icons, then I added them to the menu bar at the top.

I need to make a connection when one of them is pressed, and cannot figure out how to make a callback for it.
Should I completely create them through code or is there a way to add a callback for them (and not just make them interact with other objects).

+4
source share
2 answers

Menu bar items are action items. To do something when pressed, you need to catch the triggered() signal from the action. Read more about signals and slots here .

To do this, you need to declare a new slot in your MainWindow class. Qt also supports this automatically, without having to connect anything, but I prefer to do it myself. If you are 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 an object that contains the signal that we will listen to (menu button). The second is the name of the signal. The third is the object in which the receiving slot is located (in this case, our window). The fourth is the slot.

And also, clickMenuButton() will be called whenever an action is clicked.

As I said, Qt can also automatically connect signals to slots . The disadvantage here is that you cannot change the name of the slot, but you also do not need to connect it.

Qt Creator supports the creation of slots for widgets: if your menu acts, you should contact the form developer and you should see a list of actions in your form (if you do not, find the action editor). Right-click the action you want and click "Go" to the slot .... There, double-click triggered() .

Then Qt Creator will open a new slot in the code editor, and you can do whatever you want here!

+10
source

To do this, you need to add a QAction , add it to the menu, associate an icon with it, and then create a callback for it. I use VS Integration, so I don’t know the details of how to do this in Creator, but it should be possible without creating a file in the code.

There should be an action editor. from there you add an action, then right-click on it or something to add an icon to it, then drag it to the menu, and then maybe double-click it to create a slot for it. Here's how it works in VS integration at least.

0
source

Source: https://habr.com/ru/post/1299874/


All Articles