当前位置:网站首页>QT learning 28 toolbar in the main window
QT learning 28 toolbar in the main window
2022-07-07 07:52:00 【A little black sauce】
Qt Study 28 Toolbar in the main window
Toolbar in the main window
- The toolbar The concept and significance of
- In the application Integration of various functions An area for quick use
- Toolbar and No In the application must Existing components
- Elements in the toolbar It can be various window components
- Elements in the toolbar It usually exists in the form of icon buttons
- stay Qt Provide with The toolbar Related class components

- stay Qt Create a toolbar in the main window
/* 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);
QToolBarKey member functions ofvoid setFloatable(bool floatable)void setMovable(bool movable)void setIconSize(const QSize& iconSize)
QToolBarYou can add anyQWidgetComponents
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);
Programming experiment - First experience of toolbar
#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();
}
Programming actual combat - Toolbar practice
#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;
}
The effect is as follows :

Summary
- The toolbar is Integration of various functions A shortcut area of
- Qt Pass through
QToolBarCreate toolbars QToolBarCan join anyQWidgetComponentsQToolBarElements in are usually With icon button The way
边栏推荐
- Technology cloud report: from robot to Cobot, human-computer integration is creating an era
- 【webrtc】m98 screen和window采集
- 2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
- 通信设备商,到底有哪些岗位?
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
- Hands on deep learning (IV) -- convolutional neural network CNN
- Tongda injection 0day
- Linux server development, SQL statements, indexes, views, stored procedures, triggers
- 今日现货白银操作建议
猜你喜欢
![[SUCTF 2019]Game](/img/9c/362117a4bf3a1435ececa288112dfc.png)
[SUCTF 2019]Game
![[ANSYS] learning experience of APDL finite element analysis](/img/bc/dc0742c308816553a80d50d1a990e3.jpg)
[ANSYS] learning experience of APDL finite element analysis

mysql多列索引(组合索引)特点和使用场景

php导出百万数据

SQL优化的魅力!从 30248s 到 0.001s

After 95, Alibaba P7 published the payroll: it's really fragrant to make up this

Technology cloud report: from robot to Cobot, human-computer integration is creating an era

【Unity】物体做圆周运动的几个思路

You Li takes you to talk about C language 6 (common keywords)

《动手学深度学习》(四) -- 卷积神经网络 CNN
随机推荐
Idea add class annotation template and method template
[UTCTF2020]file header
Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
numpy中dot函数使用与解析
Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
Live online system source code, using valueanimator to achieve view zoom in and out animation effect
IPv4 exercises
leetcode:105. 从前序与中序遍历序列构造二叉树
2022焊工(初级)判断题及在线模拟考试
Leetcode-543. Diameter of Binary Tree
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
Iterable、Collection、List 的常见方法签名以及含义
JSON introduction and JS parsing JSON
Hands on deep learning (IV) -- convolutional neural network CNN
Linux server development, SQL statements, indexes, views, stored procedures, triggers
1142_ SiCp learning notes_ Functions and processes created by functions_ Linear recursion and iteration
1141_ SiCp learning notes_ Functions abstracted as black boxes
[guess-ctf2019] fake compressed packets
PHP exports millions of data
解决问题:Unable to connect to Redis