当前位置:网站首页>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中的元素通常 以图标按钮 的方式存在
边栏推荐
- 242. Bipartite graph determination
- vus. Precautions for SSR requesting data in asyndata function
- [advanced digital IC Verification] command query method and common command interpretation of VCs tool
- C语言航班订票系统
- 图解GPT3的工作原理
- vus.SSR在asynData函数中请求数据的注意事项
- Leetcode sword finger offer brush questions - day 20
- 1142_ SiCp learning notes_ Functions and processes created by functions_ Linear recursion and iteration
- buuctf misc USB
- Redis technology leak detection and filling (II) - expired deletion strategy
猜你喜欢

The metauniverse of the platofarm farm continues to expand, with Dao governance as the core

即刻报名|飞桨黑客马拉松第三期等你挑战

Hands on deep learning (IV) -- convolutional neural network CNN

@component(““)

buuctf misc USB
![[GUET-CTF2019]虚假的压缩包](/img/a2/7da2a789eb49fa0df256ab565d5f0e.png)
[GUET-CTF2019]虚假的压缩包

2022制冷与空调设备运行操作复训题库及答案
![[2022 ciscn] replay of preliminary web topics](/img/1c/4297379fccde28f76ebe04d085c5a4.png)
[2022 ciscn] replay of preliminary web topics

解决问题:Unable to connect to Redis

Tencent's one-day life
随机推荐
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
242. Bipartite graph determination
Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
科技云报道:从Robot到Cobot,人机共融正在开创一个时代
【webrtc】m98 screen和window采集
[SUCTF 2019]Game
IO流 file
【VHDL 并行语句执行】
SQL优化的魅力!从 30248s 到 0.001s
探索Cassandra的去中心化分布式架构
You Li takes you to talk about C language 6 (common keywords)
Figure out the working principle of gpt3
2022年茶艺师(中级)考试试题及模拟考试
Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
Button wizard script learning - about tmall grabbing red envelopes
Use and analysis of dot function in numpy
按键精灵采集学习-矿药采集及跑图
JSON introduction and JS parsing JSON
微信小程序中使用wx.showToast()进行界面交互
vus. Precautions for SSR requesting data in asyndata function