第二十八课、主窗口中的工具栏------------------狄泰软件学院
一、主窗口中的工具栏
1、工具栏的概念和意义
(1)、应用程序中集成各种功能实现快捷键使用的一个区域
(2)、工具栏并不是应用程序中必须存在的组件
(3)、工具栏中的元素可以是各种窗口组件
(4)、工具栏的元素通常以图标按钮的方式存在
2、在Qt中提供与工具栏相关的类的组件
3、在Qt主窗口中创建工具栏
4、QToolBar的关键成员函数
5、QToolBar可以加入任意的QWidget组件
1、初探工具栏
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtGui/QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); }; #endif // MAINWINDOW_H
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "MainWindow.h" #include <QToolBar> #include <QAction> #include <QIcon> #include <QSize> #include <QPushButton> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { /***********************1、创建工具栏并设置相关属性*********************************/ QToolBar* tb = addToolBar("ToolBar");//注意调用的是成员函数而不是构造函数 tb->setMovable(false); tb->setFloatable(false); tb->setIconSize(QSize(20, 20)); /***********************2、创建快捷键并设置相关属性*********************************/ QAction* action = new QAction("", NULL); action->setToolTip("open"); action->setIcon(QIcon(":/Res/pic/open.png"));//注意添加资源文件的方法 /***********************3、将快捷键添加到工具栏*********************************/ tb->addAction(action); //工具栏中可以添加组件 QPushButton* Btn = new QPushButton(); Btn->setText("Btn"); tb->addWidget(Btn); } MainWindow::~MainWindow() { }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <QtGui/QApplication> #include "MainWindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
2、NotePad
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMenuBar> #include <QMenu> #include <QAction> #include <QString> #include <QtGui/QMainWindow> #include <QToolBar> #include <QIcon> #include <QSize> class MainWindow : public QMainWindow { Q_OBJECT private: MainWindow(QWidget *parent = 0); MainWindow(const MainWindow& obj); MainWindow* operator = (const MainWindow& obj); bool construct(); bool initMenuBar();//菜单栏 bool initToolBar();//工具栏 bool initFileMenu(QMenuBar* mb);//文件菜单 bool initEditMenu(QMenuBar* mb);//编辑菜单 bool initFormatMenu(QMenuBar* mb);//格式菜单 bool initViewMenu(QMenuBar* mb);//视图菜单 bool initHelpMenu(QMenuBar* mb);//帮助菜单 bool initFileToolItem(QToolBar* tb); bool makeAction(QAction*& action, QString text, int ket);//菜单项 bool makeAction(QAction*& action, QString tip, QString icon); public: static MainWindow* NewInstance(); ~MainWindow(); }; #endif // MAINWINDOW_H
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "MainWindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { } bool MainWindow::construct() { bool ret = true; ret = ret && initMenuBar(); ret = ret && initToolBar(); return ret; } MainWindow* MainWindow::NewInstance() { MainWindow* ret = new MainWindow(); if((ret==NULL) || (!ret->construct())) { delete ret; ret = NULL; } return ret; } bool MainWindow::initMenuBar()//菜单栏 { bool ret = true; QMenuBar* mb = menuBar();//一定要注意是menuBar(),这是普通成员函数,不是构造函数 ret = ret && initFileMenu(mb);//传一个参数是为了在initFileMenu()函数将menu加入菜单栏 ret = ret && initEditMenu(mb); ret = ret && initFormatMenu(mb); ret = ret && initViewMenu(mb); ret = ret && initHelpMenu(mb); return ret; } bool MainWindow::initToolBar()//工具栏 { bool ret = true; QToolBar* tb = addToolBar("Tool Bar"); tb->setMovable(false); tb->setFloatable(false); tb->setIconSize(QSize(16,16)); ret = ret && initFileToolItem(tb); return ret; } /************************************************文件菜单********************************************************/ bool MainWindow::initFileMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("File(&F)");//创建文件菜单,(&F)是为了可以Alt+F打开 ret = (menu != NULL); if(ret) { QAction* action = NULL; //New ret = ret && makeAction(action, "New(&N)",Qt::CTRL + Qt::Key_N); if(ret) { menu->addAction(action); } menu->addSeparator(); //Open ret = ret && makeAction(action, "Open(&O)...",Qt::CTRL + Qt::Key_O); if(ret) { menu->addAction(action); } menu->addSeparator(); //Save ret = ret && makeAction(action, "Save(&S)",Qt::CTRL + Qt::Key_S); if(ret) { menu->addAction(action); } menu->addSeparator(); //Save As ret = ret && makeAction(action, "Save As(&A)...",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //print ret = ret && makeAction(action, "Print(&P)...",Qt::CTRL + Qt::Key_P); if(ret) { menu->addAction(action); } menu->addSeparator(); //Exit ret = ret && makeAction(action, "Exit(&X)",0); if(ret) { menu->addAction(action);//将菜单项加入到菜单 } } if(ret) { mb->addMenu(menu);//将菜单加入到菜单栏 } else { delete mb; } return ret; } /************************************************编辑菜单********************************************************/ bool MainWindow::initEditMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("Edit(&E)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Undo ret = ret && makeAction(action, "Undo(&U)",Qt::CTRL + Qt::Key_Z); if(ret) { menu->addAction(action); } menu->addSeparator(); //Redo ret = ret && makeAction(action, "Redo(&R)...",Qt::CTRL + Qt::Key_Y); if(ret) { menu->addAction(action); } menu->addSeparator(); //Cut ret = ret && makeAction(action, "Cut(&T)",Qt::CTRL + Qt::Key_X); if(ret) { menu->addAction(action); } menu->addSeparator(); //Copy ret = ret && makeAction(action, "Copy(&C)...",Qt::CTRL + Qt::Key_C); if(ret) { menu->addAction(action); } menu->addSeparator(); //Pase ret = ret && makeAction(action, "Pase(&P)...",Qt::CTRL + Qt::Key_V); if(ret) { menu->addAction(action); } menu->addSeparator(); //Delete ret = ret && makeAction(action, "Delete(&L)",Qt::Key_Delete); if(ret) { menu->addAction(action); } menu->addSeparator(); //Find ret = ret && makeAction(action, "Find(&F)...",Qt::CTRL + Qt::Key_F); if(ret) { menu->addAction(action); } menu->addSeparator(); //Replace ret = ret && makeAction(action, "Replace(&R)...",Qt::CTRL + Qt::Key_H); if(ret) { menu->addAction(action); } menu->addSeparator(); //Goto ret = ret && makeAction(action, "Goto(&G)",Qt::CTRL + Qt::Key_G); if(ret) { menu->addAction(action); } menu->addSeparator(); //Select All ret = ret && makeAction(action, "Select All(&A)",Qt::CTRL + Qt::Key_A); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /************************************************格式菜单********************************************************/ bool MainWindow::initFormatMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("Format(&O)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Auto Wrap ret = ret && makeAction(action, "Auto Wrap(&W)",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //Font ret = ret && makeAction(action, "Font(&F)...",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /************************************************视图菜单********************************************************/ bool MainWindow::initViewMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("View(&V)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Tool Bar ret = ret && makeAction(action, "Tool Bar(&T)",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //Status Bar ret = ret && makeAction(action, "Status Bar(&S)",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /************************************************帮助菜单********************************************************/ bool MainWindow::initHelpMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("Help(&H)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //User Manual ret = ret && makeAction(action, "User Manual",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //About NotePad ret = ret && makeAction(action, "About NotePad...",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } bool MainWindow::initFileToolItem(QToolBar* tb) { bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, "New", ":/Res/pic/new.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Open", ":/Res/pic/open.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Save", ":/Res/pic/save.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Save As", ":/Res/pic/saveas.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Print", ":/Res/pic/print.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Undo", ":/Res/pic/undo.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Redo", ":/Res/pic/redo.png"); if(ret) { tb->addAction(action); } return ret; } bool MainWindow::makeAction(QAction*& action, QString text, int key)//菜单项 { bool ret = true; action = new QAction(text, NULL); if(action != NULL) { action->setShortcut(QKeySequence(key));//创建快捷键 } else { ret = false; } return ret; } bool MainWindow::makeAction(QAction*& action, QString tip, QString icon) { bool ret = true; action = new QAction("", NULL); if(action != NULL) { action->setToolTip(tip); action->setIcon(QIcon(icon)); } else { ret = false; } return ret; } MainWindow::~MainWindow() { }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <QtGui/QApplication> #include "MainWindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow* w = MainWindow::NewInstance(); int ret = -1; if(w != NULL) { w->show(); ret = a.exec(); } delete w; return ret; }
二、小结
(1)、工具栏是集成各种功能实现快捷键使用的一个区域
(2)、Qt中通过QToolBar进行工具栏的创建
(3)、QToolBar可以加入任意的QWidget组件
(4)、QToolBar中的元素通常以图标按钮的方式存在