废话我就不多说了,如下:
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QMainWindow>
#include<QAction>
#include<QMenu>
#include<QToolBar>
#include<QTextEdit>
#include<QPlainTextEdit>
class mainWindow:public QMainWindow
{
Q_OBJECT
public:
mainWindow();
/*private slots:
void openSlot();
void saveSlot();
void saveAsSlot();
void newFileSlot();*/
private:
void createMenus();
QAction *openAction;
QAction *saveAction;
QAction *saveAsAction;
QAction *newFileAction;
QAction *exitAction;
QAction *cutAction;
QAction *copyAction;
QAction *pasteAction;
QAction *about;
QAction *aboutQt;
QMenu *fileMenu;
QMenu *editMenu;
QMenu *helpMenu;
QTextEdit *mainTextEdit;
};
#endif // MAINWINDOW_H
mainWindow.cpp
#include<QtGui>
#include"mainWindow.h"
mainWindow::mainWindow()
{
mainTextEdit=new QTextEdit;
setCentralWidget(mainTextEdit);
resize(300,300);
createMenus();
}
void mainWindow::createMenus()
{
fileMenu=menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newFileAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
fileMenu->addAction(exitAction);
editMenu=menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
helpMenu=menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(about);
helpMenu->addAction(aboutQt);
}
main.cpp
#include<QApplication>
#include"mainWindow.h"
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
mainWindow mainwindow;
app.setApplicationName("Text Application");
mainwindow.show();
return app.exec();
}
运行:
Starting /home/chengyan/qt/Application-build-desktop/Application...
The program has unexpectedly finished.
/home/chengyan/qt/Application-build-desktop/Application exited with code 0
请教!!!
------解决方案--------------------
你的QAction和QMenu都没有初始化,需要new一个对象。