zl程序教程

您现在的位置是:首页 >  工具

当前栏目

[Qt]状态栏QStatusBar使用

Qt 状态栏 使用
2023-09-14 09:07:12 时间

1.效果

在这里插入图片描述

在这里插入图片描述

2.代码

h文件

#ifndef MAINSATUSTEST_H
#define MAINSATUSTEST_H

#include <QtWidgets/QMainWindow>
#include "ui_mainsatustest.h"
#include "LedLabel.h"

class MainSatusTest : public QMainWindow
{
	Q_OBJECT

public:
	MainSatusTest(QWidget *parent = 0);
	~MainSatusTest();

private:
	Ui::MainSatusTestClass ui;
	LedLabel* m_status1;
	LedLabel* m_status2;
	LedLabel* m_status3;
	LedLabel* m_status4;
	LedLabel* m_status5;
	int m_cnt = 0;

private slots:
	void slot_btnColor();
	void slot_btnTxt();
};

#endif // MAINSATUSTEST_H


cpp文件

#include "mainsatustest.h"
#pragma execution_character_set("utf-8") 


MainSatusTest::MainSatusTest(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	m_status1 = new LedLabel(this, "永久信息");
	m_status2 = new LedLabel(this, "灰色");
	m_status3 = new LedLabel(this, "灰色");
	m_status4 = new LedLabel(this, "灰色");
	m_status5 = new LedLabel(this, "灰色");

	ui.stBar->addPermanentWidget(m_status1);//永久信息窗口 - 不会被一般消息覆盖
	ui.stBar->addWidget(m_status2);//正常信息窗口 - 会被showMessage()的消息覆盖
	ui.stBar->addWidget(m_status3);
	ui.stBar->addWidget(m_status4);
	ui.stBar->addWidget(m_status5);
	ui.stBar->setSizeGripEnabled(false);//去掉状态栏右下角的三角

	connect(ui.btn_color, SIGNAL(clicked()), this, SLOT(slot_btnColor()));//发送数据
	connect(ui.btn_txt, SIGNAL(clicked()), this, SLOT(slot_btnTxt()));//发送数据
}

void MainSatusTest::slot_btnTxt()
{
	ui.stBar->showMessage("临时信息...(显示3秒钟)", 3000); // 显示临时信息,时间3秒钟,不会遮盖永久窗口
}

void MainSatusTest::slot_btnColor()
{
	
	if (m_cnt > 3 || m_cnt < 0)
	{
		m_cnt = 0;
	}

	QString strShow;
	switch (m_cnt)
	{
	case 0://灰色
		strShow = "这是 灰色";
		break;
	case 1://绿色
		strShow = "这是 绿色";
		break;
	case 2://黄色
		strShow = "这是 黄色";
		break;
	case 3://红色
		strShow = "这是 红色";
		break;
	}
	m_status1->updateUI(m_cnt, "永久信息");
	m_status2->updateUI(m_cnt, strShow);
	m_status3->updateUI(m_cnt, strShow);
	m_status4->updateUI(m_cnt, strShow);
	m_status5->updateUI(m_cnt, strShow);
	m_cnt++;
}

3.扩展

Qt添加多个状态栏QStatusBar:https://blog.csdn.net/humanking7/article/details/88082382

原文:https://blog.csdn.net/humanking7/article/details/88065425