QT通過styleSheet樣式表設(shè)置按鈕背景圖片自適應(yīng)大小
默認(rèn)按鈕效果
?文章來源地址http://www.zghlxwxcb.cn/news/detail-512422.html
鼠標(biāo)移動(dòng)到按鈕上的效果
?
?
鼠標(biāo)按住不放的效果?
?文章來源:http://www.zghlxwxcb.cn/news/detail-512422.html
?
1.添加背景圖片資源
QT creator如何添加資源文件_txwtech的博客-CSDN博客項(xiàng)目文件夾右鍵-》Add New...QT creator如何添加資源文件。輸入一個(gè)名字比如:img_rc。https://txwtech.blog.csdn.net/article/details/126407743?spm=1001.2014.3001.55022.右鍵Resources下面的.qrc文件,添加現(xiàn)有文件,選擇圖片文件
styleSheet
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//background-image: url(:/button_bg.png);
QString pic_path=tr(":/button_bg.png");
QString pic_path_hover=tr(":/button_pause_icon.png");
QString styleSheet=QString("QPushButton{border-image:url(%1);background-color:transparent;border:none;}"
"QPushButton:hover{border-image:url(%2);background-color:transparent;font: 12pt \"楷體\";}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 18pt \"楷體\";}"
).arg(pic_path).arg(pic_path_hover);
ui->pushButton_reg_validate->setStyleSheet(styleSheet);
}
MainWindow::~MainWindow()
{
delete ui;
}
pushButton_reg_validate為pushButton的objectName的屬性名字
到了這里,關(guān)于QT通過styleSheet樣式表設(shè)置按鈕背景圖片自適應(yīng)大小的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!