1 Attachment(s)
Re: How to perform action on clicking a QMenu object only?
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.
Attachment 10105
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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QSignalMapper>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
void createActions();
private slots:
void help();
};
#endif // MAINWINDOW_H
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
createActions();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createActions()
{
connect(ui
->menuHelp,
SIGNAL(triggered
(QAction*)),
this,
SLOT(help
()));
}
void MainWindow::help()
{
qDebug()<<"inside help qdialog";
}
main.cpp
Code:
#include "mainwindow.h"
#include <QApplication>
#include <ui_mainwindow.h>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
Output when I click on Help QMenu, absolutely nothing:
Code:
Starting E:\Qt2\modules\guiPrototype2\build-guiPrototype2-Desktop_Qt_5_2_0_MSVC2010_32bit-Debug\debug\guiPrototype2.exe...
Added after 9 minutes:
This works:
Code:
void MainWindow::createActions()
{
[..]
connect(ui->menuHelp, SIGNAL(aboutToShow()), this, SLOT(help()));
}
void MainWindow::help()
{
qDebug()<<"inside help qdialog";
}
Re: How to perform action on clicking a QMenu object only?
If the help menu is not a menu, i.e. does't contain entries, then don't add a menu, add an action.
Cheers,
_