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...
1
  • My friend, you can add QAction directly to the QMenuBar, your menuHelp should be a QAction not a QMenu.
    – vincent
    Mar 14 at 7:36

2 Answers 2

11

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";
}
1
  • 1
    with that there is a bug here, when moving mouse on this menu, without clicking, the slot trigering automatically.
    – vivi
    Sep 30, 2019 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()));
6
  • triggered() SIGNAL doesn't work in this specific case.
    – cappy0704
    Mar 5, 2014 at 12:34
  • @SaiKamat Because you are using QMenu. Read my answer. It works as I have tested it.
    – thuga
    Mar 5, 2014 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 5, 2014 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 5, 2014 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, 2016 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.