How to connect menu click with action in Qt Creator?

前端 未结 4 1046
天命终不由人
天命终不由人 2020-12-29 01:32

I am completely new to Qt.

I started with a new Qt4 GUI Application.

Using the designer, I have created a menu like so:

File
 - Exit
<         


        
4条回答
  •  暖寄归人
    2020-12-29 01:59

    I have seen 2 may be 3 this kind of questions on this great forum but every one is very confusing,there is no need to go to signal/slot creator just got Qt Designer and follow the following steps

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

    2.Secondly add the following code in your mainwindow.cpp.

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

    3.same can be done for menus as well using following code:

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

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

    1. declare your action in your mainwindow.h as following

      QAction *myaction;

    2. and add following code in your mainwindow.cpp

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

提交回复
热议问题