mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtGui> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0); ~MainWindow(); void createMenus(); //创建菜单 void createActions(); //创建动作 void createToolBars(); //创建工具栏 public slots: void sl_NewFile(); void sl_OpenFile(); void sl_SaveFile(); void sl_About(); protected: void loadFile(QString); private: 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 *edtText; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include <QTextStream> #include <QFile> MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { setWindowTitle(tr("QMainWindow")); edtText = new QTextEdit(this); setCentralWidget(edtText); createActions(); createMenus(); createToolBars(); } MainWindow::~MainWindow() { } void MainWindow::createActions() { //"打开"动作 fileOpenAction = new QAction(QIcon("images/open.png"),tr("Open"),this); fileOpenAction->setShortcut(tr("Ctrl+0")); fileOpenAction->setStatusTip(tr("open a file")); connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(sl_OpenFile())); //"新建"动作 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(sl_NewFile())); //"保存"动作 fileSaveAction = new QAction(QIcon("images/save.png"),tr("Save"),this); fileSaveAction->setShortcut(tr("Ctrl+S")); fileSaveAction->setStatusTip(tr("save file")); connect(fileSaveAction,SIGNAL(activated()),this,SLOT(sl_SaveFile())); //"退出"动作 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("cut to clipboard"); connect(cutAction,SIGNAL(triggered()),this,SLOT(cut())); //"复制"动作 copyAction = new QAction(QIcon("images/copy.png"),tr("copy"),this); copyAction->setShortcut(tr("Ctrl+C")); copyAction->setStatusTip("copy to clipboard"); connect(copyAction,SIGNAL(triggered()),this,SLOT(copy())); //"粘贴"动作 pasteAction = new QAction(QIcon("images/paste.png"),tr("paste"),this); pasteAction->setShortcut(tr("Ctrl+V")); pasteAction->setStatusTip("paste clipboard to selection"); connect(pasteAction,SIGNAL(triggered()),this,SLOT(paste())); //"关于"动作 aboutAction = new QAction(tr("About"),this); connect(aboutAction,SIGNAL(triggered()),this,SLOT(sl_About())); } void MainWindow::createMenus() { //文件菜单 fileMenu = menuBar()->addMenu(tr("File")); fileMenu->addAction(fileNewAction); fileMenu->addAction(fileOpenAction); fileMenu->addAction(fileSaveAction); fileMenu->addSeparator(); fileMenu->addAction(exitAction); //编辑菜单 editMenu = menuBar()->addMenu(tr("Edit")); editMenu->addAction(copyAction); editMenu->addAction(cutAction); editMenu->addAction(pasteAction); //帮助菜单 aboutMenu = menuBar()->addMenu(tr("Help")); aboutMenu->addAction(aboutAction); } void MainWindow::createToolBars() { //文件工具栏 fileTool = addToolBar("File"); fileTool->addAction(fileNewAction); fileTool->addAction(fileOpenAction); fileTool->addAction(fileSaveAction); fileTool->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea); fileTool->setMovable(false); //编辑工具栏 editTool = addToolBar("Edit"); editTool->addAction(copyAction); editTool->addAction(cutAction); editTool->addAction(pasteAction); } void MainWindow::sl_NewFile() { MainWindow *newWin = new MainWindow; newWin->show(); } void MainWindow::sl_OpenFile() { QString fileName = QFileDialog::getOpenFileName(this); if(!fileName.isEmpty()) { if(edtText->document()->isEmpty()) { loadFile(fileName); } else { MainWindow *newWin = new MainWindow; newWin->show(); newWin->loadFile(fileName); } } } void MainWindow::sl_SaveFile() { } void MainWindow::sl_About() { } void MainWindow::loadFile(QString fileName) { QFile file(fileName); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream textStream(&file); while (!textStream.atEnd()) { edtText->append(textStream.readLine()); } } }
工具条是一个可移动的窗口,它可停靠的区域由QToolBar的allowAreas()函数决定的,它的参数包括:
Qt::LeftToolBarArea、Qt::RightToolBarArea、Qt::TopToolBarArea、Qt::BottomToolBarArea、Qt::AllToolBarArea.
(默认为Qt::AllToolBarArea,启动时默认出现于主窗口的顶部)。
setMovable()函数确定工具条的可移动性。默认参数为true,可移动。