How to connect to a menu using an action in Qt Creator?

I am completely new to Qt.

I started with a new Qt4 GUI application.

Using the constructor, I created a menu like this:

File - Exit 

How to get the action associated with a menu item?

I found something called the Signals and Slots editor, but I have no idea how to use it.

+45
qt action menu
Apr 28 2018-10-10T00:
source share
4 answers

Click on the green plus sign after you have selected the signal slot editor. He will give you 4 fields to fill out. For the sender, you choose what creates the signal. For example, ActionExit might be the name you created for the exit menu item. The signal is likely to be pressed (). Usually a receiver is a class that you created that has all your methods. A slot is a method that you created in this class that you want to execute. For example:

 actionExit clicked() <nameOfClass> exitGame() 

Hope this helps.

+47
Apr 28 2018-10-18T00:
source share

I managed to do it in a way that seems a lot easier. In the Qt Creator action editor window, I see an entry for the menu item that I clicked. I click this entry and select "Go to slot ...", then I select triggered () from the popup and click "OK". Qt Creator jumps me to the code that he just added ... I put the qDebug instruction there, and it works!

+41
May 21 '10 at 10:30
source share

Go to the slot editor, and then click the "Action Editor" tab on the left side. All listed menu actions.

Right-click β†’ go to slot, provides a slot function.

+4
Mar 31 '17 at 2:39 on
source share

I saw 2, maybe 3 questions of this kind in this wonderful forum, but they are all very confusing, there is no need to go to the creator of the signal / slot that Qt Designer just received, and follow these steps

1.add Menu and action in the menu bar and add any function to the slot of your mainwindow.h file as the following private slots: void help();

2. Secondly, add the following code to your mainwindow.cpp.

connect(ui->actionmyactions, SIGNAL(triggered()), this, SLOT(help()));

3. The same can be done for the menu using the following code:

connect(ui->menuHelp, SIGNAL(aboutToShow()), this, SLOT(help()));

4. You can get the desired results without going to Qt Designer as follows.

  1. declare your action in mainwindow.h as follows

    QAction *myaction;

  2. and add the following code to your mainwindow.cpp

    myaction = ui->mainToolBar->addAction("help"); connect(myaction, SIGNAL(triggered()), this, SLOT(help()));

0
Jun 27 '19 at 9:37
source share



All Articles