当前位置:   article > 正文

Qt 第29课、主窗口中的状态栏_qt状态栏

qt状态栏

1、主窗口中的状态栏

  • 状态栏的概念和意义
    — 状态栏是应用程序中输出简要信息的区域
    — 状态栏一般位于主窗口的最底部
    — 状态栏的消息类型
     实时消息:如(当前程序状态)
     永久消息:如(程序版本号,机构名称)
     进度消息:如(进度条提示,百分比提示)
  • 在 Qt 中提供与状态栏相关的类组件
    在这里插入图片描述
  • 在 Qt 主窗口中创建状态栏
    在这里插入图片描述
  • Qt 状态栏的设计原则
    左边的区域用于输出实时消息
    右边的区域用于设置永久消息
    addWidget 在状态栏的左半部分添加组件
    addPermanentWidget 在状态栏的右半部分添加组件

状态栏初体验:

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

MainWindow.cpp

#include "MainWindow.h"

#include <QStatusBar>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{
   
    QStatusBar* sb = statusBar();
    
    QLabel* label = new QLabel("label");
    QLineEdit* edit = new QLineEdit("edit");
    QPushButton* btn = new QPushButton("btn");

    sb->addPermanentWidget(label);
    sb->addPermanentWidget(edit);
    sb->addPermanentWidget(btn);
    
    sb->showMessage("xiebs");

}

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

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

在这里插入图片描述
记事本程序:

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

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

    bool construct();

    bool initMenuBar();
    bool initToolBar();
    bool initStatusBar();

    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 str, int key);
    bool makeAction(QAction*& action, QString tip, QString res);

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 <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QToolBar>
#include <QIcon>
#include <QSize>
#include <QStatusBar>
#include <QLabel>


MainWindow::MainWindow(): QMainWindow()
{
   

}

bool MainWindow::construct()
{
   
    bool ret = true;

    ret = ret && initMenuBar();
    ret = ret && initToolBar();
    ret = ret && initStatusBar();

    return ret;
}

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

    return ret;
}

bool MainWindow::initMenuBar()
{
   
    bool ret = true;
    QMenuBar* mb = menuBar();
    if(mb != nullptr)
    {
   
        ret = ret && i
  • 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
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/109812
推荐阅读
相关标签
  

闽ICP备14008679号