

综上, 学习 Qt, 其中⼀个很重要的任务就是熟悉并掌握 Qt 内置的常用控件. 这些控件对于我们快速开发出符合需求的界面, 是至关重要的.
控件是 GUI 开发中的通⽤概念. 不仅仅局限在 Qt 中.
第⼀阶段:

第⼆阶段:

第三阶段:

上图是前端中的 Element-ui 中的控件概览, ⽆论是丰富程度还是颜值, 都比Qt ⾃带的控件更胜⼀筹.

下列表格列出了 QWidget 中的属性及其作⽤.
属性 | 作⽤ |
|---|---|
enabled | 设置控件是否可使⽤. true 表⽰可⽤, false 表⽰禁⽤. |
geometry | 位置和尺⼨. 包含 x, y, width, height 四个部分. 其中坐标是以⽗元素为参考进行设置的. |
windowTitle | 设置 widget 标题. |
windowIcon | 设置 widget 图标. |
windowOpacity | 设置 widget 透明度. |
cursor | ⿏标悬停时显示的图标形状. 是普通箭头, 还是沙漏, 还是⼗字等形状. 在 Qt Designer 界⾯中可以清楚看到可选项. |
font | 字体相关属性. 涉及到字体家族, 字体大小, 粗体, 斜体, 下划线等等样式. |
toolTip | ⿏标悬停在 widget 上会在状态栏中显示的提示信息. |
toolTipDuring | toolTip 显示的持续时间. |
statusTip | Widget 状态发⽣改变时显⽰的提⽰信息(⽐如按钮被按下等). |
2.2.1:代码1
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QPushButton> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); QPushButton * MyBtn = new QPushButton(this); MyBtn->setText("按钮被禁用"); MyBtn->setEnabled(false); } Widget::~Widget() { delete ui; }
运⾏程序, 可以看到按钮处于灰⾊状态, ⽆法被点击.
2.2.2:代码2
QObject的objectName属性介绍
当前自动生成的objectname有些小规律,这个名字就是根据控件的类型 + 下划线 + 数字来生成的.很明显,以数字的方式命名,不是一个好的编程习惯,我们也可以自行修改objectname,在这里由于就两个控件,博主就不修改啦.
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
qDebug() << "按下按钮";
}
void Widget::on_pushButton_2_clicked()
{
//获取第一个按钮的可用状态
bool enable = ui->pushButton->isEnabled();
if(enable)
{
ui->pushButton->setEnabled(false);
}
else
{
ui->pushButton->setEnabled(true);
}
}


位置和尺⼨. 其实是四个属性的统称:

但是实际开发中, 我们并不会直接使⽤这⼏个属性, ⽽是通过⼀系列封装的⽅法来获取/修改.
对于 Qt 的坐标系, 不要忘记是⼀个 "左⼿坐标系". 其中坐标系的原点是当前元素的⽗元素的左上⻆.

API | 说明 |
|---|---|
geometry | 获取到控件的位置和尺寸. 返回结果是⼀个 QRect, 包含了 x, y, width, height. 其中 x, y 是左上⻆的坐标. |
setGeometry(QRect) setGeometry(int x, int y, int width, int height) | 设置控件的位置和尺寸. 可以直接设置⼀个 QRect, 也可以分四个属性单独设置 |
QRect是一个矩形,QPoint表示一个点,这两个属于是小对象,里面的属性非常少,占用空间也小 在C++中使用上面对象,通常就会按照值传递的方式来传递参数了.
控制按钮的位置
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_up_clicked();
void on_pushButton_left_clicked();
void on_pushButton_right_clicked();
void on_pushButton_down_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H#include "widget.h"
#include "ui_widget.h"
#include <QRect>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_up_clicked()
{
//获取控件的位置
QRect rect = ui->pushButton_target->geometry();
rect.setY(rect.y() - 10);
//设置控件的位置
ui->pushButton_target->setGeometry(rect);
}
void Widget::on_pushButton_left_clicked()
{
//获取控件的位置
QRect rect = ui->pushButton_target->geometry();
rect.setX(rect.x() - 10);
//设置控件的位置
ui->pushButton_target->setGeometry(rect);
}
void Widget::on_pushButton_right_clicked()
{
//获取控件的位置
QRect rect = ui->pushButton_target->geometry();
rect.setX(rect.x() + 10);
//设置控件的位置
ui->pushButton_target->setGeometry(rect);
}
void Widget::on_pushButton_down_clicked()
{
//获取控件的位置
QRect rect = ui->pushButton_target->geometry();
rect.setY(rect.y() + 10);
//设置控件的位置
ui->pushButton_target->setGeometry(rect);
}
运⾏程序, 可以看到, 按下下方的四个按钮, 就会控制 target 的左上⻆的位置. 对应的按钮整个尺寸也会发生改变.
如果想让整个按钮都移动, 可以改成下列代码:
#include "widget.h"
#include "ui_widget.h"
#include <QRect>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_up_clicked()
{
//获取控件的位置
QRect rect = ui->pushButton_target->geometry();
//rect.setY(rect.y() - 10);
//设置控件的位置
ui->pushButton_target->setGeometry(rect.x(),rect.y() - 10,rect.width(),rect.height());
}
void Widget::on_pushButton_left_clicked()
{
//获取控件的位置
QRect rect = ui->pushButton_target->geometry();
//rect.setX(rect.x() - 10);
//设置控件的位置
ui->pushButton_target->setGeometry(rect.x() - 10,rect.y(),rect.width(),rect.height());
}
void Widget::on_pushButton_right_clicked()
{
//获取控件的位置
QRect rect = ui->pushButton_target->geometry();
//rect.setX(rect.x() + 10);
//设置控件的位置
ui->pushButton_target->setGeometry(rect.x() + 10,rect.y(),rect.width(),rect.height());
}
void Widget::on_pushButton_down_clicked()
{
//获取控件的位置
QRect rect = ui->pushButton_target->geometry();
//rect.setY(rect.y() + 10);
//设置控件的位置
ui->pushButton_target->setGeometry(rect.x(),rect.y() + 10,rect.width(),rect.height());
}
往界面上拖拽两个按钮和⼀个 Label. Label 的 objectName 为 pushButton_accept 和 pushButton_reject , label 的 objectName 为 label
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_accept_clicked();
void on_pushButton_reject_pressed();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// 设置随机种子. 使用时间戳作为随机种子.
srand(time(0));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_accept_clicked()
{
ui->label->setText("女神女神, 快来嘴儿一个~, mua~");
}
void Widget::on_pushButton_reject_pressed()
{
// 如果女神点击了这个按钮, 就把这个按钮给挪走.
// 可以通过生成随机数的方式, 来确定按钮的新的位置.
//获取当前程序窗口的尺寸
int width = this->geometry().width();
int height = this->geometry().height();
// 重新生成按钮的位置.
/*
rand()是C标准库的函数,能够生成一个随机的数,这个数字范围很大
rand() % 100 + 1------>[1,100]
*/
int x = rand() % width;
int y = rand() % height;
ui->pushButton_reject->move(x,y);
}
运⾏程序, 可以看到, 当点击 "残忍拒绝" 时, 按钮就跑了.

相关 API
API | 说明 |
|---|---|
x() | 获取横坐标 计算时包含 window frame |
y() | 获取纵坐标 计算时包含 window frame |
pos() | 返回 QPoint 对象, ⾥⾯包含 x(), y(), setX(), setY() 等方法. 计算时包含 window frame |
frameSize() | 返回 QSize 对象, ⾥⾯包含 width(), height(), setWidth(), setHeight() 等方法.计算时包含 window frame |
frameGeometry() | 返回 QRect 对象. QRect 相当于 QPoint 和 QSize 的结合体. 可以获取 x, y, width, size. 计算时包含 window frame 对象. |
width() | 获取宽度 计算时 不包含 window frame |
height() | 获取⾼度 计算时 不包含 window frame |
size() | 返回 QSize 对象, ⾥⾯包含 width(), height(), setWidth(), setHeight() 等⽅法.计算时 不包含 window frame |
rect() | 返回 QRect 对象. QRect 相当于 QPoint 和 QSize 的结合体. 可以获取并设置 x, y, width, size.计算时 不包含window frame 对象. |
geometry() | 返回 QRect 对象. QRect 相当于 QPoint 和 QSize 的结合体. 可以获取 x, y, width, size. 计算时 不包含windowframe 对象. |
setGeometry() | 直接设置窗口的位置和尺寸. 可以设置 x, y, width, height, 或者 QRect 对象. 计算时 不包含windowframe 对象. |
如果把上述代码修改成打印 pushButton 的 geometry 和 frameGeometry , 结果就是完全相 同的. 因为 pushButton 并非是⼀个窗口. 2.5:windowTitle
API | 说明 |
|---|---|
windowTitle() | 获取到控件的窗口标题 |
setWindowTitle(const QString& title) | 设置控件的窗口标题. |
#include "widget.h"
#include "ui_widget.h"
#include <QWidget>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("这是标题");
}
Widget::~Widget()
{
delete ui;
}
API | 说明 |
|---|---|
windowIcon() | 获取到控件的窗⼝图标. 返回 QIcon 对象. |
setWindowIcon(const QIcon& icon | 设置控件的窗⼝图标. |
设置窗⼝图标
先在 D 盘中放⼀个图⽚, 名字为cat.png

修改 widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QIcon>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QIcon icon("D:/cat.png");
this->setWindowIcon(icon);
}
Widget::~Widget()
{
delete ui;
}运行程序, 可以看到窗口图标已经成为上述图片

对于 Qt 程序来说, 当前⼯作⽬录可能是变化的. ⽐如通过 Qt Creator 运⾏的程序, 当前⼯作⽬录是项目的构建目录; 直接双击 exe 运⾏, 工作目录则是 exe 所在目录.所谓构建⽬录, 是和 Qt 项⽬并列的, 专门⽤来放⽣成的临时⽂件和最终 exe 的目录.

获取当前的⼯作⽬录
在界⾯上创建⼀个⽐较大的 label, 确保能把路径显示完整. objectName 使⽤默认的 label 即可.
#include "widget.h"
#include "ui_widget.h"
#include <QDir>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QString CurrentPath = QDir::currentPath();
ui->label->setText(CurrentPath);
}
Widget::~Widget()
{
delete ui;
}直接在 Qt Creator 中执⾏程序, 可以看到当前⼯作⽬录是项⽬的构建⽬录.

进入上述构建目录, 把⾥⾯的 exe 拷⻉到其他目录中, 比如 D: 中. 再次执行程序, 可以看到当前⼯作目录已经发⽣改变.

要想直接能双击 exe 运⾏, 需要先把 Qt 的路径添加到 path 环境变量中, 否则会提⽰找不到动态库.在最开始搭建开发环境的时候已经操作过, 此处不再赘述.
注意, 上述 构建目录, 是随时可删除的. 比如点击菜单栏中的 "构建" -> "清理项目" , 就会把这个目录中的内容清空掉. 因此如果我们把图片文件放到构建⽬录中, 可能在不小心删除后就丢失了. 我们还是希望能够把图片和源代码放到⼀起, 并且使我们的程序无论到任何位置中都能正确使用图片.
Qt 使用qrc 机制帮我们自动完成了上述工作, 更⽅便的来管理项目依赖的静态资源.
在项目中创建一个qrc文件,文件名不要带中文和特殊符号.


先添加一个前缀,前缀的名字改成 / 即可

将图片导入到资源文件中.

这个按钮在创建前缀之前是禁用的,创建好前缀后就能够使用了,添加的文件就是添加到前缀下面的,点击添加文件得到的目录就是当前代码所在的目录.

导入图片的时候,需要确保导入的图片在resource.qrc文件的同级目录,或者同级目录的子目录里.因此就可以把需要导入的图片拷贝到项目目录中即可~

看到这个效果,说明导入成功了,创建的前缀叫啥名字,代码中就叫啥名字.并且当代码中需要访问qrc中管理的文件时,就需要在路径上带有:前缀~~
#include "widget.h"
#include "ui_widget.h"
#include <QIcon>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QIcon icon(":/Cat.png");
this->setWindowIcon(icon);
}
Widget::~Widget()
{
delete ui;
}

当Qt项目进行编译的时候,这个cpp文件就被一起编译到了exe中,当exe程序运行的时候,上述图片的数据也就被加载到了内存中.
API | 说明 |
|---|---|
windowOpacity() | 获取到控件的不透明数值. 返回 float, 取值为 0.0 -> 1.0 其中 0.0表示全透明, 1.0表示完全不透明. |
setWindowOpacity(flo at n) | 设置控件的不透明数值. |
在界面上拖放两个按钮, 分别⽤来增加不透明度和减少不透明度.objectName 分别为 pushButton_add 和 pushButton_sub.

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_add_clicked()
{
float Opacity = this->windowOpacity();
//0.0表示完全透明,1.0表示完全不透明
if(Opacity >= 1.0)
return;
qDebug()<< Opacity;
Opacity += 0.1;
this->setWindowOpacity(Opacity);
}
void Widget::on_pushButton_sub_clicked()
{
float Opacity = this->windowOpacity();
//0.0表示完全透明,1.0表示完全不透明
if(Opacity <= 0.0)
return;
qDebug()<< Opacity;
Opacity -= 0.1;
this->setWindowOpacity(Opacity);
}

执行程序, 可以看到, 点击了几下 - 之后, 就可以透过窗口看到后⾯的蓝色背景了. 点击 + 又会逐渐恢复.同时控制台中也可以看到 opacity 数值的变化.
注意, C++ 中 float 类型遵守 IEEE 754 标准, 因此在进⾏运算的时候会有⼀定的精度误差. 因此 1 - 0.1 的数值并非是 0.9.

API | 说明 |
|---|---|
cursor() | 获取到当前 widget 的 cursor 属性, 返回 QCursor 对象.当⿏标悬停在该 widget 上时, 就会显示出对应的形状. |
setCursor(const QCursor& cursor) | 设置该 widget 光标的形状. 仅在⿏标停留在该widget 上时⽣效. |
QGuiApplication::setOverrideCursor(co nst QCursor& cursor) | 设置全局光标的形状. 对整个程序中的所有 widget 都会生效.覆盖.上面的setCursor 设置的内容. |
QGuiApplication::setOverrideCursor(const QCursor& cursor)设置全局光标指的是系统内的全局光标,而不是系统级的全局.
在 Qt Designer 中设置按钮的光标
在界面中创建⼀个按钮.

直接在右侧属性编辑区修改 cursor 属性为 "等待"

运⾏程序, ⿏标悬停到按钮上, 即可看到光标的变化
截图无法截到鼠标光标, uu们自行运行验证
#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QPushButton * MyButton = new QPushButton(this);
MyButton->move(100,100);
MyButton->setText("这是一个按钮");
//设置按钮的Cursor
MyButton->setCursor(QCursor(Qt::OpenHandCursor));
}
Widget::~Widget()
{
delete ui;
}系统内置的光标形状如下
enum CursorShape {
ArrowCursor,
UpArrowCursor,
CrossCursor,
WaitCursor,
IBeamCursor,
SizeVerCursor,
SizeHorCursor,
SizeBDiagCursor,
SizeFDiagCursor,
SizeAllCursor,
BlankCursor,
SplitVCursor,
SplitHCursor,
PointingHandCursor,
ForbiddenCursor,
WhatsThisCursor,
BusyCursor,
OpenHandCursor,
ClosedHandCursor,
DragCopyCursor,
DragMoveCursor,
DragLinkCursor,
LastCursor = DragLinkCursor,
BitmapCursor = 24,
CustomCursor = 25
}⾃定义鼠标光标 Qt ⾃带的光标形状有限. 我们也可以自己找个图片, 做成⿏标的光标. 比如我们有请猫猫⽼铁

创建 qrc 资源⽂件, 添加前缀 / , 并加入Cat .png



#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QPixmap pixmap(":/Cat.png");
//通过这个函数对图片进行播放,注意播放不是修改图片对象本身,而是返回一个新的图片对象副本
pixmap = pixmap.scaled(70,70);
// 创建 QCursor 对象, 并指定 "热点" 为 (2, 2) 坐标位置.
// 所谓 "热点" 就是⿏标点击时⽣效的位置.
QCursor cursor(pixmap,2,2);
this->setCursor(cursor);
}
Widget::~Widget()
{
delete ui;
}API | 说明 |
|---|---|
font() | 获取当前 widget 的字体信息. 返回 QFont对象. |
setFont(const QFont& font) | 设置当前 widget 的字体信息. |
关于 QFont.
属性 | 说明 |
|---|---|
family | 字体家族. 比如 "楷体", "宋体", "微软雅⿊" 等. |
pointSize | 字体大小. |
weight | 字体粗细. 以数值⽅式表示粗细程度取值范围为 [0, 99], 数值越⼤, 越粗. |
bold | 是否加粗. 设置为 true, 相当于 weight 为 75. 设置为 false 相当于 weight 为 50. |
italic | 是否倾斜. |
underline | 是否带有下划线. |
strikeOut | 是否带有删除线. |
在 Qt Designer 中设置字体属性
在界面上创建⼀个 label;

在右侧的属性编辑区, 设置该 label 的 font 相关属性

执行程序, 观察效果

#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->label->setText("Hello QT");
//创建字体对象
QFont font;
//设置字体家族
font.setFamily("仿宋");
//设置字体大小
font.setPointSize(20);
//设置字体加粗
font.setBold(true);
//设置字体倾斜
font.setItalic(true);
//设置下划线
font.setUnderline(true);
//设置删除线
font.setStrikeOut(true);
//设置字体对象到Label上
ui->label->setFont(font);
}
Widget::~Widget()
{
delete ui;
}
API | 说明 |
|---|---|
setToolTip | 设置 toolTip. ⿏标悬停在该 widget 上时会有提示说明. |
setToolTipDuring | 设置 toolTip 提⽰的时间. 单位 ms. 时间到后 toolTip ⾃动消失. |
toolTip 只是给用户看的. 在代码中⼀般不需要获取到 toolTip.
在界⾯上拖放两个按钮. objectName 设置为 pushButton_yes 和 pushButton_no

#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->pushButton_yes->setToolTip("这是YES按钮");
ui->pushButton_yes->setToolTipDuration(3000);
ui->pushButton_no->setToolTip("这是NO按钮");
ui->pushButton_no->setToolTipDuration(3000);
}
Widget::~Widget()
{
delete ui;
}
设置控件获取到焦点的策略. 比如某个控件能否用鼠标选中或者能否通过 tab 键选中.


API | 说明 |
|---|---|
focusPolicy() | 获取该widget 的 focusPolicy, 返回 Qt::FocusPolicy |
setFocusPolicy(Qt::FocusPolicy policy | 设置 widget 的 focusPolicy |
Qt::FocusPolicy 是⼀个枚举类型. 取值如下.
在界⾯上创建四个单行输入框 (Line Edit)

修改四个输入框的 focusPolicy 属性为 Qt::StrongFocus (默认取值, ⼀般不⽤额外修改)

此时运⾏程序, 可以看到, 使⽤鼠标单击/tab, 就可以移动光标所在输⼊框. 从⽽接下来的输⼊就是针对这个获取焦点的输入框展开的了.

修改第⼆个输入框的 focusPolicy 为 Qt::NoFocus , 则第⼆个输⼊框不会被 tab / 鼠标左键 选中.

此时这个输入框也就⽆法输⼊内容了.
修改第⼆个输入框 focusPolicy 为 Qt::TabFocus , 则只能通过 tab 选中, ⽆法通过⿏标选 中.

通过 CSS 设置 widget 的样式.
CSS 中可以设置的样式属性非常多. 基于这些属性 Qt 只能支持其中⼀部分, 称为 QSS (Qt Style Sheet) 具体的⽀持情况可以参考 Qt ⽂档中 "Qt Style Sheets Reference" 章节.
在界面上创建 label

编辑右侧的 styleSheet 属性, 设置样式

编辑完成样式之后, 可以看到在 Qt Designer 中能够实时预览出效果

在界面上创建⼀个多行输入框 (Text Edit) 和两个按钮. objectName 分别为 pushButton_light 和 pushButton_dark

编写按钮的 slot 函数. #333 是深⾊, 但是没那么黑. #fff 是纯白色. #000 是纯黑色.
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_light_clicked()
{
this->setStyleSheet("background-color: #f3f3f3");
ui->textEdit->setStyleSheet("background-color:#fff;color:#000;");
ui->pushButton_light->setStyleSheet("color:#000");
ui->pushButton_dark->setStyleSheet("color:#000");
}
void Widget::on_pushButton_dark_clicked()
{
this->setStyleSheet("background-color: #333");
ui->textEdit->setStyleSheet("background-color:#333;color:#fff;");
ui->pushButton_light->setStyleSheet("color:#fff");
ui->pushButton_dark->setStyleSheet("color:#fff");
}计算机中使⽤ "像素" 表示屏幕上的⼀个基本单位(也就是⼀个发亮的光点). 每个光点都使⽤三个字节表示颜色, 分别是 R (red), G (green), B (blue) ⼀个字节表示 (取值范围是 0-255, 或者 0x00-0xFF). 混合三种不同颜⾊的数值⽐例, 就能搭配出千千万万的颜⾊出来.
当然, 上述规则只是针对⼀般的程序而言是这么设定的. 实际的显示器, 可能有 8bit ⾊深或者 10bit色深等, 实际情况会更加复杂
运行程序, 点击 "日间模式", 就是浅⾊背景, 深⾊文字; 点击 "夜间模式", 就是深⾊背景, 浅色文字.

