当前位置:网站首页>Qt学习28 主窗口中的工具栏
Qt学习28 主窗口中的工具栏
2022-07-07 04:31:00 【一个小黑酱】
Qt学习28 主窗口中的工具栏
主窗口中的工具栏
- 工具栏的概念和意义
- 应用程序中 集成各种功能 实现快捷使用的一个区域
- 工具栏并 不是 应用程序中 必须 存在的组件
- 工具栏中的元素 可以是各种窗口组件
- 工具栏中的元素 通常以图标按钮的方式存在
- 在 Qt 中提供与 工具栏 相关的类组件

- 在 Qt 主窗口中创建工具栏
/* call member function */
QToolBar *tb = addToolBar("Tool Bar");
/* create item for Tool Bar */
QAction *action = new QAction("", NULL);
/* set action property */
action->setToolTip("Open");
action->setIcon(QIcon(":/Res/pic/open.png"));
/* add item to Tool Bar */
tb->addAction(action);
QToolBar的关键成员函数void setFloatable(bool floatable)void setMovable(bool movable)void setIconSize(const QSize& iconSize)
QToolBar中可以加入任意的QWidget组件
QToolBar *tb = addToolBar("Tool Bar");
QPushButton *b = new QPushButton("Button");
QLabel *l = new QLabel("Label");
QLineEdit *e = new QLineEdit();
tb->addWidget(b);
tb->addWidget(l);
tb->addWidget(e);
编程实验 - 工具栏的初体验
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
#include "MainWindow.h"
#include <QToolBar>
#include <QAction>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QToolBar *tb = addToolBar("Tool Bar");
QAction *action = new QAction("", NULL);
tb->setFloatable(false);
tb->setMovable(false);
action->setToolTip("Open");
action->setIcon(QIcon(":/res/open.png"));
tb->addAction(action);
QPushButton* b = new QPushButton("Button");
QLabel* l = new QLabel("Label");
QLineEdit* e = new QLineEdit();
tb->addWidget(b);
tb->addWidget(l);
tb->addWidget(e);
}
MainWindow::~MainWindow()
{
}
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
编程实战 - 工具栏实战
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAction>
#include <QMenu>
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
MainWindow();
bool construct();
bool initMenuBar();
bool initToolBar();
bool initFileToolItem(QToolBar* tb);
bool initEditToolItem(QToolBar *tb);
bool initFileMenu(QMenuBar* mb);
bool initEditMenu(QMenuBar* mb);
bool initFormatMenu(QMenuBar* mb);
bool initViewMenu(QMenuBar* mb);
bool initHelpMenu(QMenuBar* mb);
bool makeAction(QToolBar *tb, QString tip, QString icon);
bool makeAction(QMenu* menu, QString text, int key=0);
public:
static MainWindow* NewInstance();
~MainWindow();
};
#endif // MAINWINDOW_H
#include "MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QToolBar>
#include <QSize>
MainWindow::MainWindow()
{
}
MainWindow *MainWindow::NewInstance()
{
MainWindow* ret = new MainWindow();
if ((ret == NULL) || !ret->construct()) {
delete ret;
ret = NULL;
}
return ret;
}
bool MainWindow::construct()
{
bool ret = true;
ret = ret && initMenuBar();
ret = ret && initToolBar();
return ret;
}
bool MainWindow::initToolBar()
{
bool ret = true;
QToolBar* tb = addToolBar("Tool Bar");
tb->setIconSize(QSize(16, 16));
ret = ret && initFileToolItem(tb);
tb->addSeparator();
ret = ret && initEditToolItem(tb);
return ret;
}
bool MainWindow::initFileToolItem(QToolBar *tb)
{
bool ret = true;
ret = ret && makeAction(tb, "New", ":/res/pic/new.png");
ret = ret && makeAction(tb, "Open", ":/res/pic/open.png");
ret = ret && makeAction(tb, "Save", ":/res/pic/save.png");
ret = ret && makeAction(tb, "Save as", ":/res/pic/saveas.png");
ret = ret && makeAction(tb, "Print", ":/res/pic/print.png");
return ret;
}
bool MainWindow::initEditToolItem(QToolBar *tb)
{
bool ret = true;
ret = ret && makeAction(tb, "undo", ":/res/pic/undo.png");
ret = ret && makeAction(tb, "redo", ":/res/pic/redo.png");
return ret;
}
bool MainWindow::initMenuBar()
{
bool ret = true;
QMenuBar* mb = menuBar();
ret = ret && initFileMenu(mb);
ret = ret && initEditMenu(mb);
ret = ret && initFormatMenu(mb);
ret = ret && initViewMenu(mb);
ret = ret && initHelpMenu(mb);
return ret;
}
bool MainWindow::initFileMenu(QMenuBar* mb)
{
bool ret = true;
QMenu* menu = new QMenu("File(&F)");
ret = (menu != NULL);
if (ret) {
ret = ret && makeAction(menu, "New(N)", Qt::CTRL + Qt::Key_N);
ret = ret && makeAction(menu, "New Window(W)", Qt::CTRL + Qt::SHIFT + Qt::Key_N);
ret = ret && makeAction(menu, "Open(O)", Qt::CTRL + Qt::Key_O);
ret = ret && makeAction(menu, "Save(S)", Qt::CTRL + Qt::Key_S);
ret = ret && makeAction(menu, "Save as(S)", Qt::CTRL + Qt::SHIFT + Qt::Key_S);
menu->addSeparator();
ret = ret && makeAction(menu, "Page setting(U)");
ret = ret && makeAction(menu, "Print(P)", Qt::CTRL + Qt::Key_P);
menu->addSeparator();
ret = ret && makeAction(menu, "Exit(X)", Qt::CTRL + Qt::Key_X);
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::initEditMenu(QMenuBar *mb)
{
bool ret = true;
QMenu* menu = new QMenu("Edit(&E)");
ret = (menu != NULL);
if (ret) {
ret = ret && makeAction(menu, "Undo(U)", Qt::CTRL + Qt::Key_Z);
ret = ret && makeAction(menu, "Clip(T)", Qt::CTRL + Qt::Key_X);
ret = ret && makeAction(menu, "Copy(C)", Qt::CTRL + Qt::Key_C);
ret = ret && makeAction(menu, "Stick(C)", Qt::CTRL + Qt::Key_V);
ret = ret && makeAction(menu, "Delete(L)", Qt::Key_Delete);
menu->addSeparator();
ret = ret && makeAction(menu, "Use Bing search", Qt::CTRL + Qt::Key_E);
ret = ret && makeAction(menu, "Find(F)", Qt::CTRL + Qt::Key_F);
ret = ret && makeAction(menu, "Find next(N)", Qt::Key_F3);
ret = ret && makeAction(menu, "Find pre(V)", Qt::SHIFT + Qt::Key_F3);
ret = ret && makeAction(menu, "Replace(R)", Qt::CTRL + Qt::Key_H);
ret = ret && makeAction(menu, "Replace(R)", Qt::CTRL + Qt::Key_H);
ret = ret && makeAction(menu, "Jump(G)", Qt::CTRL + Qt::Key_G);
menu->addSeparator();
ret = ret && makeAction(menu, "Select all(A)", Qt::CTRL + Qt::Key_A);
ret = ret && makeAction(menu, "Data(D)", Qt::Key_F5);
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::initFormatMenu(QMenuBar *mb)
{
bool ret = true;
QMenu* menu = new QMenu("Format(&O)");
ret = (menu != NULL);
if (ret) {
ret = ret && makeAction(menu, "Auto wrap(W)");
ret = ret && makeAction(menu, "Font(F)");
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::initViewMenu(QMenuBar *mb)
{
bool ret = true;
QMenu* menu = new QMenu("View(&V)");
ret = (menu != NULL);
if (ret) {
ret = ret && makeAction(menu, "Zoom(Z)");
ret = ret && makeAction(menu, "Status bar(S)");
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::initHelpMenu(QMenuBar *mb)
{
bool ret = true;
QMenu* menu = new QMenu("Help(&H)");
ret = (menu != NULL);
if (ret) {
ret = ret && makeAction(menu, "View Help(H)");
ret = ret && makeAction(menu, "Send feedback(F)");
ret = ret && makeAction(menu, "About App(A)");
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::makeAction(QToolBar *tb, QString tip, QString icon)
{
bool ret = true;
QAction *action = new QAction("", NULL);
if (action != NULL) {
action->setToolTip(tip);
action->setIcon(QIcon(icon));
}
else {
ret = false;
}
if (ret) {
tb->addAction(action);
}
return ret;
}
bool MainWindow::makeAction(QMenu *menu, QString text, int key)
{
bool ret = true;
QAction* action = new QAction(text, NULL);
if (action != NULL) {
if (key != 0) {
action->setShortcut(QKeySequence(key));
}
}
else {
ret = false;
}
if (ret) {
menu->addAction(action);
}
return ret;
}
MainWindow::~MainWindow()
{
}
#include "MainWindow.h"
#include <QApplication>
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;
}
效果如下:

小结
- 工具栏是 集成各种功能 的一个快捷区域
- Qt 中通过
QToolBar进行工具栏的创建 QToolBar能够加入任意的QWidget组件QToolBar中的元素通常 以图标按钮 的方式存在
边栏推荐
- [advanced digital IC Verification] command query method and common command interpretation of VCs tool
- numpy中dot函数使用与解析
- [2022 ciscn] replay of preliminary web topics
- Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
- 《动手学深度学习》(四) -- 卷积神经网络 CNN
- gatk4中的interval是什么??
- mysql多列索引(组合索引)特点和使用场景
- LeetCode 90:子集 II
- Most elements
- 【webrtc】m98 screen和window采集
猜你喜欢

MobaXterm

php导出百万数据

Resource create package method
![[webrtc] m98 Screen and Window Collection](/img/b1/1ca13b6d3fdbf18ff5205ed5584eef.png)
[webrtc] m98 Screen and Window Collection
![[mathematical notes] radian](/img/43/2af510adb24fe46fc0033d11d60488.jpg)
[mathematical notes] radian

misc ez_usb
![[2022 CISCN]初赛 web题目复现](/img/1c/4297379fccde28f76ebe04d085c5a4.png)
[2022 CISCN]初赛 web题目复现

The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
![[guess-ctf2019] fake compressed packets](/img/a2/7da2a789eb49fa0df256ab565d5f0e.png)
[guess-ctf2019] fake compressed packets

Use and analysis of dot function in numpy
随机推荐
Resource create package method
Sign up now | oar hacker marathon phase III, waiting for your challenge
解决could not find or load the Qt platform plugin “xcb“in ““.
C语言航班订票系统
Is the test cycle compressed? Teach you 9 ways to deal with it
【经验分享】如何为visio扩展云服务图标
Jenkins remote build project timeout problem
Use and analysis of dot function in numpy
测试周期被压缩?教你9个方法去应对
numpy中dot函数使用与解析
1140_ SiCp learning notes_ Use Newton's method to solve the square root
Tianqing sends instructions to bypass the secondary verification
2022焊工(初级)判断题及在线模拟考试
Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
[P2P] local packet capturing
Why is the row of SQL_ The ranking returned by number is 1
What are the positions of communication equipment manufacturers?
[2022 actf] Web Topic recurrence
解决:Could NOT find KF5 (missing: CoreAddons DBusAddons DocTools XmlGui)
Why should we understand the trend of spot gold?