当前位置:   article > 正文

Qt 第28课、主窗口中的工具栏_qt 工具栏

qt 工具栏

1、主窗口中的工具栏

  • 工具栏的概念和意义
    — 应用程序中 集成各种功能 实现快捷使用的一个区域
    — 工具栏并不是应用程序中必须存在的组件
    — 工具栏中的元素可以是各种窗口组件
    — 工具栏中的元素通常以图标按钮的方式存在

  • 在 Qt 中提供与工具栏相关的类组件
    在这里插入图片描述

  • 在 Qt 主窗口中创建工具栏
    在这里插入图片描述

  • QToolBar 的关键成员函数
    void setFloatable(bool floatable)      设置当前工具栏能不能悬浮,参数一般设置成 false
    void setMovable(bool movable)       设置当前工具栏能不能移动,参数一般设置成 false
    void setIconSize(const QSize& iconSize)   设置工具栏的每一个图标大小

  • QToolBar 中可以加入任意的QWidget 组件
    在这里插入图片描述

工具栏初体验

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
   
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
};
#endif // MAINWINDOW_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

MainWindow.cpp

#include "MainWindow.h"
#include "QToolBar"
#include <QAction>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{
   
    QToolBar* tb = addToolBar("ToolBar");
    QAction* action = new QAction("", nullptr);

    action->setToolTip("Open");

    action->setIcon(QIcon(":/Res/open.png"));

    QPushButton* btn = new QPushButton("btn");
    QLabel* label = new QLabel("label");
    QLineEdit* edit = new QLineEdit("xiebs");

    tb->setFloatable(false);
    tb->setMovable(false);

    tb->addAction(action);
    tb->addWidget(btn);
    tb->addWidget(label);
    tb->addWidget(edit);
}

MainWindow::~MainWindow()
{
   
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

main.cpp

#include "MainWindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
   
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

工具栏实例:
MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMenuBar>
#include <QToolBar>
#include <QMenu>
#include <QAction>

class MainWindow : public QMainWindow
{
   
    Q_OBJECT
private:
    MainWindow();
    MainWindow(const MainWindow&);
    MainWindow& operator= (const MainWindow&);

    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 initEditToolItem(QToolBar* tb);
    bool initFormatToolItem(QToolBar* tb);
    bool initViewToolItem(QToolBar* tb);

    bool makeAction(QAction*& action, QString text, int key);
    bool makeAction(QAction*& action, QString tip, QString icon);
public:
    static MainWindow* NewInstance();
    ~MainWindow();
};
#endif // MAINWINDOW_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

MainWindow.cpp

#include "MainWindow.h"
#include <QIcon>

MainWindow::MainWindow()
{
   
    resize(500, 400);
}

MainWindow* MainWindow::NewInstance()
{
   
    MainWindow* ret = new MainWindow();
    if(!(ret && ret->construct()))
    {
   
        delete ret;
        ret = nullptr;
    }
    return ret;
}

bool MainWindow::construct()
{
   
    bool ret = true;
    ret = ret && initMenuBar();
    ret = ret && initToolBar();
    return ret;
}

bool MainWindow::initMenuBar()
{
   </
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/962481
推荐阅读
相关标签
  

闽ICP备14008679号