这是GUI的快照。我想通过单击QMenu对象“帮助”来执行简单操作。这个QMenu对象没有子菜单。
你能告诉我如何在仅单击QMenu时执行操作吗?
这是我尝试过的,但输出为空。
mainwindow.h
这是我尝试过的,但输出为空。
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();
}
当我点击“帮助”QMenu
时,什么也没有输出:
Starting E:\Qt2\modules\guiPrototype2\build-guiPrototype2-Desktop_Qt_5_2_0_MSVC2010_32bit-Debug\debug\guiPrototype2.exe...
aboutToShow()
更适合。http://qt-project.org/doc/qt-5.0/qtwidgets/qmenu.html#aboutToShow - Najzero