Qt5自定义状态栏QStatusBar外观(背景)和状态栏基本用法(显示普通消息、临时消息、永久消息)

原创

Qt5自定义状态栏QStatusBar外观(背景)和状态栏基本用法(显示普通消息、临时消息、永久消息)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/naibozhuan3744/article/details/102642222
            </div>
                                                <!--一个博主专栏付费入口-->
                      <!--一个博主专栏付费入口结束-->
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-833878f763.css">
                                    <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-833878f763.css">
            <div class="htmledit_views" id="content_views">
                                        <p>本文主要总结Qt状态栏QSatatuBar用法,通过继承状态栏,自定义状态栏背景图形,以及状态栏三种基本用法。</p>

状态栏类QStatusBar主要有显示普通消息、显示定时消息、显示永久消息三种功能。三种都十分常用,下面先总结自定义状态栏外观用法和将状态栏三种用法进行详细讲解和代码编写。

一、自定义状态栏外观

1.1原理详解

状态栏类QStatusBar继承于QWidget,我们继承QStatusBar,然后在QStatusBar界面上不用布局添加相关控件,进行外观设置。

如下图所示,要实现这种四个角落有边角时,可以通过QPainter事件重绘,但是一种更简单方式是不用布局类,直接在QStatusBar界面里面的四个角落添加四个图片,就可以轻易实现这种效果。这种方式能够实现任意图形层叠设置。

 

1.2实现代码。分别添加一个类,继承QStatusBar,然后在qcustomsatusbar.h、qcustomsatusbar.cpp添加如下代码

qcustomsatusbar.h


 
 
  1. #ifndef QCUSTOMSATUSBAR_H
  2. #define QCUSTOMSATUSBAR_H
  3. #include <QWidget>
  4. #include <QStatusBar>
  5. #include <QLabel>
  6. class QCustomSatusBar : public QStatusBar
  7. {
  8. public:
  9. explicit QCustomSatusBar(QWidget *parent=nullptr);
  10. void initUI();
  11. private:
  12. QSize size;
  13. QLabel *leftTopLbl;
  14. QLabel *leftBottomLbl;
  15. QLabel *rightTopLbl;
  16. QLabel *rightBottomLbl;
  17. protected:
  18. void resizeEvent(QResizeEvent *event);
  19. };
  20. #endif // QCUSTOMSATUSBAR_H

 

qcustomsatusbar.cpp


 
 
  1. #include "qcustomsatusbar.h"
  2. QCustomSatusBar::QCustomSatusBar(QWidget *parent):QStatusBar(parent)
  3. {
  4. initUI();
  5. }
  6. void QCustomSatusBar::initUI()
  7. {
  8. this->resize( 640, 320);
  9. this->setSizeGripEnabled( false); //屏蔽右下角有伸缩功能的小三角形
  10. size=QSize( 20, 20);
  11. //初始化
  12. leftTopLbl= new QLabel( this);
  13. leftBottomLbl= new QLabel( this);
  14. rightTopLbl= new QLabel( this);
  15. rightBottomLbl= new QLabel( this);
  16. leftTopLbl->setPixmap(QPixmap( ":/new/prefix1/resource/image/leftTop.png"));
  17. leftBottomLbl->setPixmap(QPixmap( ":/new/prefix1/resource/image/leftBottom.png"));
  18. rightTopLbl->setPixmap(QPixmap( ":/new/prefix1/resource/image/rightTop.png")); //QCustomSatusBar rightBottom rightTop
  19. rightBottomLbl->setPixmap(QPixmap( ":/new/prefix1/resource/image/rightBottom.png"));
  20. leftTopLbl->setScaledContents( true);
  21. leftBottomLbl->setScaledContents( true);
  22. rightTopLbl->setScaledContents( true);
  23. rightBottomLbl->setScaledContents( true);
  24. leftTopLbl->show();
  25. leftBottomLbl->show();
  26. rightTopLbl->show();
  27. rightBottomLbl->show();
  28. }
  29. void QCustomSatusBar::resizeEvent(QResizeEvent *event)
  30. {
  31. Q_UNUSED(event);
  32. //设置四个角落图标大小
  33. leftTopLbl->setFixedSize(size);
  34. leftBottomLbl->setFixedSize(size);
  35. rightTopLbl->setFixedSize(size);
  36. rightBottomLbl->setFixedSize(size);
  37. //设置四个图标位置,随着窗口尺寸变化而变化
  38. leftTopLbl->setGeometry( 0, 0,leftTopLbl->width(),leftTopLbl->height());
  39. leftBottomLbl->setGeometry( 0,height()-leftBottomLbl->height(),leftBottomLbl->width(),leftBottomLbl->height());
  40. rightTopLbl->setGeometry(width()-rightTopLbl->width(), 0,rightTopLbl->width(),rightTopLbl->height());
  41. rightBottomLbl->setGeometry(width()-rightBottomLbl->width(),height()-rightBottomLbl->height(),rightBottomLbl->width(),rightBottomLbl->height());
  42. }

1.3调用方法

在任意一个初始化界面构造函数中,添加如下代码


 
 
  1. #include <QVBoxLayout>
  2. #include <QLabel>
  3. #include "qcustomsatusbar.h"
  4. void Widget::setupUI()
  5. {
  6. //初始化,调用自定义状态栏
  7. customSatusBar= new QCustomSatusBar();
  8. customSatusBar->setFixedHeight( 100);
  9. customSatusBar->setObjectName( "QCustomSatusBar");
  10. customSatusBar->setStyleSheet( "#QCustomSatusBar{background-color:rgba(123,41,36,0.5);}");
  11. QVBoxLayout *vlayout= new QVBoxLayout;
  12. vlayout->setMargin( 0);
  13. vlayout->setSpacing( 0);
  14. vlayout->addStretch();
  15. vlayout->addWidget(customSatusBar);
  16. this->setLayout(vlayout);
  17. }

1.4总结

之后可以将该类当成QStatusBar一样调用就行,这个类跟状态栏QStatusBar的主要区别在于背景形状不一样,其它都一样设置和调用。

 

二、状态栏QStatusBar显示普通消息

2.1原理讲解

状态栏类QStatusBar显示普通消息时,主要用如下函数。

void QStatusBar::addWidget(QWidget *widget, int stretch = 0);
 
 

用该函数可以添加QWidget控件和各种继承QWidget的子类。显示时,添加的控件QWidget固定左对齐,好像不能调整,博主调整过,但是没有效果。

2.2示例代码用法如下,在按钮槽函数添加如下代码


 
 
  1. void Widget::on_pushButton_clicked()
  2. {
  3. //状态栏显示一般信息,只能显示在最左边
  4. QLabel *showNormalInfo= new QLabel( "显示一般信息",customSatusBar);
  5. showNormalInfo->setFixedSize( 150, 50);
  6. showNormalInfo->setFrameShape(QFrame::WinPanel); //设置标签状态
  7. showNormalInfo->setFrameShadow(QFrame::Sunken); //设置阴影
  8. showNormalInfo->setGeometry( 100, 0, 100, 200);
  9. customSatusBar->addWidget(showNormalInfo);
  10. showNormalInfo->show();
  11. }

三、状态栏QStatusBar显示临时消息

3.1原理讲解

显示临时消息主要用到函数

void QStatusBar::showMessage(const QString &text, int timeout = 0);
 
 

该函数直接添加一个字符串,显示在整个状态栏里面,并且覆盖所有普通显示的QWidget控件,消息出现在状态栏最顶层界面。同样的,该消息在状态栏显示时,也是固定左对齐,好像不能调整。该函数第二个参数是定时时间,单位为毫秒,也就是设置为1000,则停留一秒钟,然后切换到普通显示界面。

3.2效果图如下所示

 

3.3调用代码如下


 
 
  1. void Widget::on_pushButton_2_clicked()
  2. {
  3. //显示暂时信息,只能显示在最左边
  4. customSatusBar->showMessage( "显示临时信息", 3000); //默认显示在左边
  5. }

四、状态栏QStatusBar显示永久消息

4.1原理讲解

状态栏显示永久消息时,一般用于显示固定的内容,比如公司信息(logo、链接地址)、产品版本说明等。永久消息在状态栏的显示默认是右对齐,核心函数只有一个。并且永久消息不会被临时消息覆盖,一直显示状态栏在最顶层。

void QStatusBar::addPermanentWidget(QWidget *widget, int stretch = 0);
 
 

4.2调用方式如下


 
 
  1. void Widget::on_pushButton_3_clicked()
  2. {
  3. //显示永久信息,只能显示在最右边
  4. QLabel *permanentInfo= new QLabel(customSatusBar);
  5. permanentInfo->setFrameStyle(QFrame::Box|QFrame::Sunken); //设置框架风格:盒子和阴影
  6. permanentInfo->setText(QString( "<a href=\"http://easyforensics.com\">永久信息:易取证</a>"));
  7. permanentInfo->setOpenExternalLinks( true);
  8. permanentInfo->setFixedHeight( 30);
  9. customSatusBar->addPermanentWidget(permanentInfo);
  10. }

4.3效果图如下

 

五、完整工程实例代码

5.1新建一个Widget工程,勾选UI,然后添加一个继承QStatusBar的类QCustomSatusBar,在UI界面拖入三个按钮,分别命名如下图所示:

 

5.2分别在qcustomsatusbar.h、qcustomsatusbar.cpp、widget.h、widget.cpp、main.cpp中,添加如下代码

qcustomsatusbar.h


 
 
  1. #ifndef QCUSTOMSATUSBAR_H
  2. #define QCUSTOMSATUSBAR_H
  3. #include <QWidget>
  4. #include <QStatusBar>
  5. #include <QLabel>
  6. class QCustomSatusBar : public QStatusBar
  7. {
  8. public:
  9. explicit QCustomSatusBar(QWidget *parent=nullptr);
  10. void initUI();
  11. private:
  12. QSize size;
  13. QLabel *leftTopLbl;
  14. QLabel *leftBottomLbl;
  15. QLabel *rightTopLbl;
  16. QLabel *rightBottomLbl;
  17. protected:
  18. void resizeEvent(QResizeEvent *event);
  19. };
  20. #endif // QCUSTOMSATUSBAR_H

 

qcustomsatusbar.cpp


 
 
  1. #include "qcustomsatusbar.h"
  2. QCustomSatusBar::QCustomSatusBar(QWidget *parent):QStatusBar(parent)
  3. {
  4. initUI();
  5. }
  6. void QCustomSatusBar::initUI()
  7. {
  8. this->resize( 640, 320);
  9. this->setSizeGripEnabled( false); //屏蔽右下角有伸缩功能的小三角形
  10. size=QSize( 20, 20);
  11. //初始化
  12. leftTopLbl= new QLabel( this);
  13. leftBottomLbl= new QLabel( this);
  14. rightTopLbl= new QLabel( this);
  15. rightBottomLbl= new QLabel( this);
  16. leftTopLbl->setPixmap(QPixmap( ":/new/prefix1/resource/image/leftTop.png"));
  17. leftBottomLbl->setPixmap(QPixmap( ":/new/prefix1/resource/image/leftBottom.png"));
  18. rightTopLbl->setPixmap(QPixmap( ":/new/prefix1/resource/image/rightTop.png")); //QCustomSatusBar rightBottom rightTop
  19. rightBottomLbl->setPixmap(QPixmap( ":/new/prefix1/resource/image/rightBottom.png"));
  20. leftTopLbl->setScaledContents( true);
  21. leftBottomLbl->setScaledContents( true);
  22. rightTopLbl->setScaledContents( true);
  23. rightBottomLbl->setScaledContents( true);
  24. leftTopLbl->show();
  25. leftBottomLbl->show();
  26. rightTopLbl->show();
  27. rightBottomLbl->show();
  28. }
  29. void QCustomSatusBar::resizeEvent(QResizeEvent *event)
  30. {
  31. Q_UNUSED(event);
  32. //设置四个角落图标大小
  33. leftTopLbl->setFixedSize(size);
  34. leftBottomLbl->setFixedSize(size);
  35. rightTopLbl->setFixedSize(size);
  36. rightBottomLbl->setFixedSize(size);
  37. //设置四个图标位置,随着窗口尺寸变化而变化
  38. leftTopLbl->setGeometry( 0, 0,leftTopLbl->width(),leftTopLbl->height());
  39. leftBottomLbl->setGeometry( 0,height()-leftBottomLbl->height(),leftBottomLbl->width(),leftBottomLbl->height());
  40. rightTopLbl->setGeometry(width()-rightTopLbl->width(), 0,rightTopLbl->width(),rightTopLbl->height());
  41. rightBottomLbl->setGeometry(width()-rightBottomLbl->width(),height()-rightBottomLbl->height(),rightBottomLbl->width(),rightBottomLbl->height());
  42. }

 

widget.h


 
 
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include "qcustomsatusbar.h"
  4. #include <QWidget>
  5. namespace Ui {
  6. class Widget;
  7. }
  8. class Widget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit Widget(QWidget *parent = 0);
  13. ~Widget();
  14. void setupUI();
  15. private slots:
  16. void on_pushButton_clicked();
  17. void on_pushButton_2_clicked();
  18. void on_pushButton_3_clicked();
  19. private:
  20. Ui::Widget *ui;
  21. QCustomSatusBar *customSatusBar;
  22. };
  23. #endif // WIDGET_H

 

widget.cpp


 
 
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QVBoxLayout>
  4. #include <QLabel>
  5. Widget::Widget(QWidget *parent) :
  6. QWidget(parent),
  7. ui( new Ui::Widget)
  8. {
  9. ui->setupUi( this);
  10. setupUI();
  11. }
  12. Widget::~Widget()
  13. {
  14. delete ui;
  15. }
  16. void Widget::setupUI()
  17. {
  18. //初始化,调用自定义状态栏
  19. customSatusBar= new QCustomSatusBar();
  20. customSatusBar->setFixedHeight( 100);
  21. customSatusBar->setObjectName( "QCustomSatusBar");
  22. customSatusBar->setStyleSheet( "#QCustomSatusBar{background-color:rgba(123,41,36,0.5);}");
  23. QVBoxLayout *vlayout= new QVBoxLayout;
  24. vlayout->setMargin( 0);
  25. vlayout->setSpacing( 0);
  26. vlayout->addStretch();
  27. vlayout->addWidget(customSatusBar);
  28. this->setLayout(vlayout);
  29. }
  30. void Widget::on_pushButton_clicked()
  31. {
  32. //状态栏显示一般信息,只能显示在最左边
  33. QLabel *showNormalInfo= new QLabel( "显示一般信息",customSatusBar);
  34. showNormalInfo->setFixedSize( 150, 50);
  35. showNormalInfo->setFrameShape(QFrame::WinPanel); //设置标签状态
  36. showNormalInfo->setFrameShadow(QFrame::Sunken); //设置阴影
  37. showNormalInfo->setGeometry( 100, 0, 100, 200);
  38. customSatusBar->addWidget(showNormalInfo);
  39. showNormalInfo->show();
  40. }
  41. void Widget::on_pushButton_2_clicked()
  42. {
  43. //显示暂时信息,只能显示在最左边
  44. customSatusBar->showMessage( "显示临时信息", 3000); //默认显示在左边
  45. }
  46. void Widget::on_pushButton_3_clicked()
  47. {
  48. //显示永久信息,只能显示在最右边
  49. QLabel *permanentInfo= new QLabel(customSatusBar);
  50. permanentInfo->setFrameStyle(QFrame::Box|QFrame::Sunken); //设置框架风格:盒子和阴影
  51. permanentInfo->setText(QString( "<a href=\"http://easyforensics.com\">永久信息:易取证</a>"));
  52. permanentInfo->setOpenExternalLinks( true);
  53. permanentInfo->setFixedHeight( 30);
  54. customSatusBar->addPermanentWidget(permanentInfo);
  55. }

 

main.cpp


 
 
  1. #include "widget.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Widget w;
  7. w.show();
  8. return a.exec();
  9. }

5.2程序运行结果如下所示

 

 

 

参考内容:

https://blog.csdn.net/oShouQianShou/article/details/68924825(参考:状态栏QSatusBar三种状态代码)

文章最后发布于: 2019-10-19 22:43:02
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值