zoukankan      html  css  js  c++  java
  • Qt-Button使用QSS变图片按钮

    相关资料:

    https://blog.csdn.net/singleroot/article/details/42492483   原文

    https://download.csdn.net/download/zhujianqiangqq/13131409   代码包下载

    mainwinodw.cpp

     1 #include "mainwindow.h"
     2 #include "ui_mainwindow.h"
     3 
     4 
     5 void SetButtonStyle(QPushButton *button, QString imgsrc, int CutSec)
     6 {
     7     int img_w=QPixmap(imgsrc).width();
     8     int img_h=QPixmap(imgsrc).height();
     9     int PicWidth = img_w/CutSec;
    10     button->setFixedSize(PicWidth,img_h);
    11     button->setStyleSheet(QString("QPushButton{border- 41px; border-image: url(%1)  0 0 0 %2 repeat  repeat;border- 0px; border-radius: 0px;}")
    12       .append("QPushButton::hover{border-image: url(%1) 0 0 0 %3  repeat  repeat;}")
    13       .append("QPushButton::pressed{border-image: url(%1) 0  0 0 %4 repeat  repeat;}")
    14       .append("QPushButton::checked{border-image: url(%1) 0  0 0 %4 repeat  repeat;}")
    15       .append("QPushButton::disabled{border-image: url(%1) 0  0 0 %5 repeat  repeat;}")
    16       .arg(imgsrc).arg(0).arg(PicWidth*1).arg(PicWidth*2).arg(PicWidth*3));
    17 }
    18 
    19 MainWindow::MainWindow(QWidget *parent)
    20     : QMainWindow(parent)
    21     , ui(new Ui::MainWindow)
    22 {
    23     ui->setupUi(this);
    24 
    25     // 一张图片切图的效果
    26     SetButtonStyle(ui->pushButton,":/new/prefix1/image/d.png",4);
    27     // 三张独立的图片
    28     ui->pushButton_2->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/a.png);}"
    29                                   "QPushButton:hover{border-image: url(:/new/prefix1/image/b.png);}"
    30                                   "QPushButton:pressed{border-image: url(:/new/prefix1/image/c.png);}");
    31 }
    32 
    33 MainWindow::~MainWindow()
    34 {
    35     delete ui;
    36 }
    View Code

    PS:一个扁平按钮QSS样式

     1     ui->pushButton_2->setStyleSheet("QPushButton{"
     2                                     "border: 1px solid #C0C0C0;"
     3                                     "background-color:#C0C0C0;"
     4                                     "border-radius:10px;"
     5                                     "}"
     6                                     "QPushButton:hover{"
     7                                     "border: 1px solid #E3C46F;"
     8                                     "background-color:#FEF4BF;"
     9                                     "border-radius:10px;"
    10                                     "}"
    11                                     "QPushButton:pressed{"
    12                                     "border: 1px solid #AAB4C4;"
    13                                     "background-color:#EAF0FF;"
    14                                     "border-radius:10px;"
    15                                     "}"
    16                                     );
    View Code
  • 相关阅读:
    python3中类(class)的一些概念
    python 第三方库paramiko
    阿里云盘PC版开放了
    解决c#,wpf程序带环境安装包体积太大问题
    【利用静态网站传输数据】
    【.net】创建属于自己的log组件——改进版
    ThingsBoard 3.2.2 发布
    mac 安装pip2
    cocos creator2.4.3 内存优化总结
    cocos creator2.4.3 组件 节点 预制体
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/14013063.html
Copyright © 2011-2022 走看看