How to perform action on clicking a QMenu object only?
Asked Answered
D

2

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...
Disyllable answered 5/3, 2014 at 12:15 Comment(1)
My friend, you can add QAction directly to the QMenuBar, your menuHelp should be a QAction not a QMenu.Vaulted
D
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";
}
Divorcee answered 5/3, 2014 at 12:21 Comment(1)
with that there is a bug here, when moving mouse on this menu, without clicking, the slot trigering automatically.Threadfin
N
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()));
Nicaea answered 5/3, 2014 at 12:31 Comment(6)
triggered() SIGNAL doesn't work in this specific case.Disyllable
@SaiKamat Because you are using QMenu. Read my answer. It works as I have tested it.Nicaea
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. :)Disyllable
@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.Nicaea
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.Remsen
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.Allrud

© 2022 - 2024 — McMap. All rights reserved.