初学QT , 请各位达人帮忙看看哪里写错了 ? 谢谢啦
用QT做一个多文档工具。
mainwindow.h
-------------------------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include "ui_mainwindow.h"
#include "QWorkspace"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
~MainWindow();
private:
//Ui::MainWindowClass ui;
QWorkspace * workspace;
public:
void CreateMenu(void);
public slots:
void PopMsg(void);
};
#endif // MAINWINDOW_H
=====================================================
mainwindow.cpp
------------------------
#include "mainwindow.h"
#include <QTextEdit>
#include <QAction>
#include <QMenuBar>
#include "QMessageBox"
MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
workspace = new QWorkspace;
setCentralWidget(workspace);
CreateMenu();
QMainWindow * win1 = new QMainWindow;
win1->setWindowTitle(tr("window 1"));
QTextEdit *edit1 = new QTextEdit;
edit1->setText(tr("window 1"));
win1->setCentralWidget(edit1);
QMainWindow * win2 = new QMainWindow;
win2->setWindowTitle(tr("window 2"));
QTextEdit *edit2 = new QTextEdit;
edit2->setText(tr("window 2"));
win2->setCentralWidget(edit2);
QMainWindow * win3 = new QMainWindow;
win3->setWindowTitle(tr("window 3"));
QTextEdit *edit3 = new QTextEdit;
edit3->setText(tr("window 3"));
win3->setCentralWidget(edit3);
workspace->addWindow(win1);
workspace->addWindow(win2);
workspace->addWindow(win3);
}
MainWindow::~MainWindow()
{
}
void MainWindow::CreateMenu(void)
{
QMenu *layoutMenu = menuBar()->addMenu(tr("layout"));
QMenu *menu_000 = menuBar()->addMenu(tr("aaa"));
QMenu *menu_001 = menuBar()->addMenu(tr("bbb"));
QAction *arrange = new QAction(tr("Hit"),this);
QAction *action_000 = new QAction(tr("aaa_hello"),this);
QAction *action_001 = new QAction(tr("bbb_hello"),this);
connect(arrange, SIGNAL(triggered()) ,workspace, SLOT(arrangeIcons()));
connect(action_000, SIGNAL(triggered()) ,workspace, SLOT(PopMsg()));
connect(action_001, SIGNAL(triggered()) ,workspace, SLOT(PopMsg()));
layoutMenu->addAction(arrange);
menu_000->addAction(action_000);
menu_001->addAction(action_001);
}
void MainWindow::PopMsg(void)
{
QMessageBox::about(NULL,"title","content");
}
==========================================================================================
问题来了: 编译没问题 , 运行没问题 ,但是在选择了 Menu 里的某个选项 ,点击后不弹出消息框。
也就是 PopMsg() 函数从未执行过,但我确实已经在connect里绑定了啊。
请大家帮忙看看哪里错了?
谢谢
[解决办法]
将
connect(arrange, SIGNAL(triggered()) ,workspace, SLOT(arrangeIcons()));
connect(action_000, SIGNAL(triggered()) ,workspace, SLOT(PopMsg()));
connect(action_001, SIGNAL(triggered()) ,workspace, SLOT(PopMsg()));
connect(arrange, SIGNAL(triggered()) ,this, SLOT(arrangeIcons()));
connect(action_000, SIGNAL(triggered()) ,this, SLOT(PopMsg()));
connect(action_001, SIGNAL(triggered()) ,this, SLOT(PopMsg()));