7

Here's a snapshot of the GUI. I want to perform simple actions solely by clicking on QMenu object Help. This QMenu object does NOT have any submenus.Perform action when Help menu is clicked

Can you please advise me how to perform actions when only the QMenu is clicked Here's what I have tried, but I got an empty output.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QSignalMapper>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    void createActions();
    QSignalMapper *pSignalMapper;

private slots:
    void help();

};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    createActions();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::createActions()
{
    pSignalMapper = new QSignalMapper(this);
    connect(ui->menuHelp, SIGNAL(triggered(QAction*)), this, SLOT(help()));

}

void MainWindow::help()
{
    qDebug()<<"inside help qdialog";
}

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <ui_mainwindow.h>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

Output when I click on Help QMenu, absolutely nothing:

Starting E:\Qt2\modules\guiPrototype2\build-guiPrototype2-Desktop_Qt_5_2_0_MSVC2010_32bit-Debug\debug\guiPrototype2.exe...
Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
cappy0704
  • 557
  • 2
  • 9
  • 30
  • 1
    well, I guess your connect() did not succeed, could you please check the return value of connect? if it is false, then the connect did not work. Plus I think `aboutThoShow()´ is a better fit http://qt-project.org/doc/qt-5.0/qtwidgets/qmenu.html#aboutToShow – Najzero Mar 05 '14 at 12:20

2 Answers2

10

I would try to do the following:

void MainWindow::createActions()
{
    [..]
    connect(ui->menuHelp, SIGNAL(aboutToShow()), this, SLOT(help()));
}

void MainWindow::help()
{
    qDebug()<<"inside help qdialog";
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • 1
    with that there is a bug here, when moving mouse on this menu, without clicking, the slot trigering automatically. – vivi Sep 30 '19 at 15:56
8

The reason it doesn't work, is because you are not triggering any action.

This signal is emitted when an action in a menu belonging to this menubar is triggered as a result of a mouse click; action is the action that caused the signal to be emitted.

What you should do is add an action to your QMenuBar instead of a QMenu.

QAction *helpAction = ui->menuBar->addAction("Help");
connect(helpAction, SIGNAL(triggered()), this, SLOT(help()));
thuga
  • 12,601
  • 42
  • 52
  • triggered() SIGNAL doesn't work in this specific case. – cappy0704 Mar 05 '14 at 12:34
  • @SaiKamat Because you are using `QMenu`. Read my answer. It works as I have tested it. – thuga Mar 05 '14 at 12:37
  • thank u, thuga. i agree with u, triggered() works when we have QAction. In this case I apologize I for not mentioning that there are no QAction items. I was looking for a method to work with QMenu only, as mentioned. :) – cappy0704 Mar 05 '14 at 12:43
  • 2
    @SaiKamat I know. I was trying to point out that you should use `QAction` instead of `QMenu`. Using `QMenu` object like a `QAction` object is an ugly hacky way of doing things. `QMenu` is a menu widget, and that's what it should do. Provide a menu. – thuga Mar 05 '14 at 12:55
  • 1
    This is neater than the accepted answer. With the QMenu::aboutToShow() signal, the menu bar remains selected after I clicked the menu control, which spoils the user experience a bit. This method works more smoothly. – d11 Oct 13 '16 at 13:16
  • Thank you, this definitely works! After creating a new `QMenuBar` object, one can use `QMainWindow::setMenuBar` to set it as the menu bar of the window. – Param Siddharth Aug 15 '20 at 10:45