QT:label标签/进度条的使用

devtools/2024/9/24 0:20:39/

文章目录

  • 设置不同格式的文本
  • 显示图片
  • 文本对齐/自动换行/缩进/边距
  • LCDNumber
    • 倒计时
  • ProgressBar
    • 进度条

设置不同格式的文本

在文本格式中,存在富文本,makedown格式的文本,还有纯文本,下面就依据这三个进行举例

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);// 纯文本格式ui->label->setTextFormat(Qt::PlainText);ui->label->setText("这是纯文本");// 富文本格式ui->label_2->setTextFormat(Qt::RichText);ui->label_2->setText("<b>这是富文本</b>");// markdown格式ui->label_3->setTextFormat(Qt::MarkdownText);ui->label_3->setText("### 这是markdown");
}Widget::~Widget()
{delete ui;
}

在这里插入图片描述

显示图片

在label标签的用法中,还有一个用法是可以使用标签来显示图片的信息:

#include "widget.h"
#include "ui_widget.h"
#include <QLabel>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);// 先创建一个labelQLabel* label = new QLabel(this);// 设置label的大小属性QRect windowrect = this->geometry();label->setGeometry(0, 0, windowrect.width(), windowrect.height());// 把图片放进去label->setPixmap(QPixmap(":/project.jpg"));
}Widget::~Widget()
{delete ui;
}

运行结果为:

在这里插入图片描述
但是现在的问题是,当窗口变化时,这个图片不会随着窗口的变化而变化 :

在这里插入图片描述

所以一种可行的方案是,重写虚函数,通过重写resizeEvent函数来改变图片大小

这样做的原理是,当窗口大小改变这个事件触发的时候,就会执行这个函数,那么此时再把图片进行大小更改即可

#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
#include <QResizeEvent>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);// 设置label的大小属性QRect windowrect = this->geometry();ui->label->setGeometry(0, 0, windowrect.width(), windowrect.height());// 把图片放进去ui->label->setPixmap(QPixmap(":/project.jpg"));
}Widget::~Widget()
{delete ui;
}void Widget::resizeEvent(QResizeEvent *event)
{ui->label->setGeometry(0, 0, event->size().width(), event->size().height());
}

在这里插入图片描述
此时发现依旧没有完成目的,这是因为没有设置图片的自动缩放的功能,这里再加一句话:

在这里插入图片描述

这样就实现好了!

文本对齐/自动换行/缩进/边距

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);// 设置文字居中对齐ui->label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);ui->label->setText("这是一段文字");// 设置自动换行ui->label_2->setAlignment(Qt::AlignTop | Qt::AlignLeft);ui->label_2->setWordWrap(true);ui->label_2->setText("这是很长的文字这是很长的文字这是很长的文字这是很长的文字这是很长的文字这是很长的文字这是很长的文字");// 设置缩进ui->label_3->setAlignment(Qt::AlignTop | Qt::AlignLeft);ui->label_3->setIndent(50);}Widget::~Widget()
{delete ui;
}

设置伙伴

创建两个标签和radiobutton:

在这里插入图片描述

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);// 在这里设置伙伴ui->label->setBuddy(ui->radioButton);ui->label_2->setBuddy(ui->radioButton_2);
}Widget::~Widget()
{delete ui;
}

LCDNumber

倒计时

在界面上创建一个倒计时的工具:

#include "widget.h"
#include "ui_widget.h"
#include <QTimer>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);// 创建一个QTimer实例qtime = new QTimer(this);// 连接到信号槽connect(qtime, &QTimer::timeout, this, &Widget::handler);qtime->start(1000);
}Widget::~Widget()
{delete ui;
}void Widget::handler()
{// 获取当前时间int value = ui->lcdNumber->intValue();// 把新时间设置进去if(value <= 0){qtime->stop();return;}ui->lcdNumber->display(value - 1);
}

在这里插入图片描述

ProgressBar

进度条

设置进度条,按时间增长

#include "widget.h"
#include "ui_widget.h"
#include <QTimer>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);timer = new QTimer(this);connect(timer, &QTimer::timeout, this, &Widget::handler);timer->start(100);
}Widget::~Widget()
{delete ui;
}void Widget::handler()
{int value = ui->progressBar->value();if(value >= 100){timer->stop();return;}ui->progressBar->setValue(value + 1);
}

在这里插入图片描述


http://www.ppmy.cn/devtools/31887.html

相关文章

JDK14特性

JDK14 1 概述2 语法层面的变化1_instanceof的模式匹配(预览)2_switch表达式(标准)3_文本块改进(第二次预览)4_Records 记录类型(预览 JEP359) 3 API层面的变化4 关于GC1_G1的NUMA内存分配优化2_弃用SerialCMS,ParNewSerial Old3_删除CMS4_ZGC on macOS and Windows 4 其他变化1…

中仕公考:2024年火箭军军队文职招考公告

一、招考岗位 火箭军相关单位从事装备维修、物资器材储存、后勤保障、汽车驾驶等工作的岗位。具体招考岗位计划见附件。 二、报考条件 年龄条件&#xff1a;年龄为18周岁-35周岁(1988年6月至2006年5月期间出生); 学历条件&#xff1a;应具有岗位要求的相应学历; 报考人员取…

关于链表带环问题为什么要用快慢指针

判断链表是否带环 题目描述 给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到达&#xff0c;则链表中存在环。 为了表示给定链表中的环&#xff0c;评测系统内部使用整数 pos 来表示链表尾连…

msyql数据库创建可以远程登录的用户和重置root密码

创建新用户&#xff0c;只能本地连接&#xff0c;无法远程登录。 CREATE USER new_userlocalhost IDENTIFIED BY password; GRANT ALL PRIVILEGES ON *.* TO new_userlocalhost; FLUSH PRIVILEGES; 创建新用户&#xff0c;并授权相关数据库的权限给用户。 GRANT SELECT, INS…

【代码随想录】day49

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 一、121. 买卖股票的最佳时机二、122.买卖股票的最佳时机II 一、121. 买卖股票的最佳时机 class Solution { public:int maxProfit(vector<int>& prices…

vcontact2:病毒聚类(失败)

Bitbucket 安装 mamba create --name vContact2 biopython1.78 mamba install -c bioconda vcontact20.11.3vim ~/envs/vContact2/lib/python3.9/site-packages/vcontact2/exports/summaries.py 把 np.warnings.filterwarnings(ignore) 改成 import warnings warnings.filte…

任何内核都无法启动解决方案

背景 实验中不停编译新的内核又懒得删除了&#xff0c;于是乎在编译到第9716个内核后&#xff0c;无法启动了。 报错如下&#xff1a; 主要是这句报错&#xff1a; 解决方案 ubuntu linux开机进入不了系统的解决办法 进入Recovery Mode打开root shell失败&#xff1a; 一…

docker pull失败:x509: certificate has expired or is not yet

centos按照官方教程安装了docker&#xff0c;拉取镜像时却出现错误&#xff1a;x509: certificate has expired or is not yet valid. 更新系统解决了问题&#xff1a; yum update yum upgrade reboot