一.针对于具体label
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//设置label1字体颜色、背景色
ui->label_1->setStyleSheet("QLabel{color:rgb(0,255,255);background-color:red;}");
}
二.针对于所有label
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//设置所有label字体颜色、背景色
this->setStyleSheet("QLabel{color:rgb(0,255,255);background-color:red;}");
}
三.设置背景图片
ui->label_2->setStyleSheet("QLabel{color:white;background-image:url(:/new/prefix1/dog.jpg)}");
虽然成功设置背景,但是问题是反复平铺的。
将background-image改为border-image就没问题了。
ui->label_2->setStyleSheet("QLabel{color:white;border-image:url(:/new/prefix1/dog.jpg)}");
其他设置:
四.边框设置
//设置按钮边框大小、颜色
ui->pushButton->setStyleSheet("QPushButton{border:2px outset green;}");
五.设置按钮状态
//border-image设置按钮背景颜色、hover鼠标放在按钮上状态、pressed鼠标点击按钮状态
ui->pushButton->setStyleSheet("QPushButton{border-image:url(:/new/prefix1/1645759305696.jpg);}"
"QPushButton:hover{border-image:url(:/new/prefix1/dog.jpg);}"
"QPushButton:pressed{border-image:url(:/new/prefix1/qq.jpg);}"
);