1.
2. mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QWorkspace>
#include <QMdiArea>
#include <QMdiSubWindow> class QAction;
class QMenu;
class QToolBar;
class QTextEdit; class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(); void createMenus();
void createActions();
void createToolBars();
void loadFile(QString fileName); public slots:
void slotNewFile();
void slotOpenFile();
void slotSaveFile();
void slotCopy();
void slotCut();
void slotPaste();
void slotAbout(); protected:
//void closeEvent(QCloseEvent *); private:
QTextCodec *codec;
QMenu *fileMenu;
QMenu *editMenu;
QMenu *aboutMenu;
QToolBar *fileTool;
QToolBar *editTool;
QAction *fileOpenAction;
QAction *fileNewAction;
QAction *fileSaveAction;
QAction *exitAction;
QAction *copyAction;
QAction *cutAction;
QAction *pasteAction;
QAction *aboutAction; QTextEdit * text; QWorkspace *workSpace;
}; #endif // MAINWINDOW_H
3. mainwindow.cpp
MainWindow构造函数中可加入 setAttribute(Qt::WA_DeleteOnClose);
多窗口时关闭窗口默认是隐藏,这样设置后为关闭,防止内存泄露。
#include "mainwindow.h" #include <QtGui> // 翋敦諳妗珋
MainWindow::MainWindow()
{
setWindowTitle(tr("myMainWindow"));
text = new QTextEdit(this);
setCentralWidget(text); /* workSpace = new QWorkspace;
setCentralWidget(workSpace);
QMainWindow *window1 = new QMainWindow;
window1->setWindowTitle("window1");
QTextEdit *text1 = new QTextEdit;
text1->setText("text1");
window1->setCentralWidget(text1);
workSpace->addWindow(window1);
*/
createActions();
createMenus();
createToolBars();
} void
MainWindow::createActions()
{ // file open action
fileOpenAction = new QAction(QIcon(":/images/open.png"),tr("Open"),this); // 湖羲恅璃
fileOpenAction->setShortcut(tr("Ctrl+O"));
fileOpenAction->setStatusTip(tr("open a file"));
connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(slotOpenFile())); // file new action
fileNewAction = new QAction(QIcon(":/images/new.png"),tr("New"),this); // 陔膘恅璃
fileNewAction->setShortcut(tr("Ctrl+N"));
fileNewAction->setStatusTip(tr("new file"));
connect(fileNewAction,SIGNAL(triggered()),this,SLOT(slotNewFile())); // save file action
fileSaveAction = new QAction(QPixmap(":/images/save.png"),tr("Save"),this); // 悵湔恅璃
fileSaveAction->setShortcut(tr("Ctrl+S"));
fileSaveAction->setStatusTip(tr("save file"));
connect(fileSaveAction,SIGNAL(activated()),this,SLOT(slotSaveFile())); // exit action
exitAction = new QAction(tr("Exit"), this); // 豖堤
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("exit"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); cutAction = new QAction(QIcon(":/images/cut.png"), tr("Cut"), this); // 熟
cutAction->setShortcut(tr("Ctrl+X"));
cutAction->setStatusTip(tr("cut to clipboard"));
connect(cutAction, SIGNAL(triggered()), text, SLOT(cut())); copyAction = new QAction(QIcon(":/images/copy.png"), tr("Copy"), this); // 葩秶
copyAction->setShortcut(tr("Ctrl+C"));
copyAction->setStatusTip(tr("copy to clipboard"));
connect(copyAction, SIGNAL(triggered()), text, SLOT(copy())); pasteAction = new QAction(QIcon(":/images/paste.png"), tr("Paste"), this); // 梜泂
pasteAction->setShortcut(tr("Ctrl+V"));
pasteAction->setStatusTip(tr("paste clipboard to selection"));
connect(pasteAction, SIGNAL(triggered()), text, SLOT(paste())); aboutAction = new QAction(tr("About"), this); // 壽衾
connect(aboutAction, SIGNAL(triggered()), this, SLOT(slotAbout()));
} void
MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("File"));
editMenu = menuBar()->addMenu(tr("Edit"));
aboutMenu = menuBar()->addMenu(tr("Help")); fileMenu->addAction(fileNewAction);
fileMenu->addAction(fileOpenAction);
fileMenu->addAction(fileSaveAction);
fileMenu->addAction(exitAction); editMenu->addAction(copyAction);
editMenu->addAction(cutAction);
editMenu->addAction(pasteAction); aboutMenu->addAction(aboutAction);
} void
MainWindow::createToolBars()
{ fileTool = addToolBar(tr("File"));
fileTool->setMovable(false);
editTool = addToolBar(tr("Edit")); fileTool->addAction(fileNewAction);
fileTool->addAction(fileOpenAction);
fileTool->addAction(fileSaveAction); editTool->addAction(copyAction);
editTool->addAction(cutAction);
editTool->addAction(pasteAction); } void MainWindow::slotNewFile()
{
MainWindow *newWin = new MainWindow();
newWin->show();
} void MainWindow::loadFile(QString fileName)
{
QFile file( fileName );
if ( file.open (QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream textStream( &file );
while( !textStream.atEnd() )
{
text->append( textStream.readLine() );
}
}
} void MainWindow::slotOpenFile()
{
QString fileName = QFileDialog::getOpenFileName(this);
if ( !fileName.isEmpty() )
{
if( text->document()->isEmpty() )
loadFile(fileName);
else
{
MainWindow *newWin = new MainWindow;
newWin->show();
newWin->loadFile(fileName);
}
}
} void MainWindow::slotSaveFile()
{
} void MainWindow::slotCopy()
{
} void MainWindow::slotCut()
{
} void MainWindow::slotPaste()
{
} void MainWindow::slotAbout()
{
}