zoukankan      html  css  js  c++  java
  • QT中给各控件增加背景图片(可缩放可旋转)的几种方法

    http://blog.csdn.net/liukang325/article/details/44832397

    1. 给QPushButton 增加背景图片:背景图片可根据Button大小自由缩放。

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. void setButtonBackImage(QPushButton *button,QString image,int sizeW, int sizeH)  
    2. {  
    3.     //163,163为原始分辨率,这里稍做了调整。  
    4.     QPixmap pixmap(image);  
    5.     QPixmap fitpixmap=pixmap.scaled(163,163).scaled(sizeW, sizeH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  
    6.     button->setIcon(QIcon(fitpixmap));  
    7.     button->setIconSize(QSize(sizeW,sizeH));  
    8.     button->setFlat(true);//就是这句能够实现按钮透明,用png图片时很有用  
    9.     button->setStyleSheet("border: 0px");//消除边框,取消点击效果  
    10. }  


    2. 给QWidget 增加背景图片:图片可自由缩放。

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. this->setAutoFillBackground(true);    //Widget增加背景图片时,这句一定要。  
    2. QPixmap pixmap(":/images/bg_news.png");  
    3. QPixmap fitpixmap=pixmap.scaled(1200, 1200).scaled(config->mainWindowW,config->mainWindowH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  
    4. QPalette palette;  
    5. palette.setBrush(QPalette::Background, QBrush(fitpixmap));  
    6. this->setPalette(palette);  


    3. 给QLabel 增加背景图片:图片可自由缩放。

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. QPixmap pixmap(normalIcon);  
    2. QPixmap fitpixmap=pixmap.scaled(labelIcon->width(), labelIcon->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  
    3. labelIcon->setPixmap(fitpixmap);  

    4. 采用QSS样式,增加背景图片,图片显示原始比例。

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. lastBtn->setStyleSheet("background-image: url(:/images/btn_previous_normal.png);border: 0px");  

    QPixmap旋转图片:

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
      1. QMatrix leftmatrix;  
      2. leftmatrix.rotate(270);  
      3. ui->label->setPixmap(pixmap.transformed(leftmatrix,Qt::SmoothTransformation));  
  • 相关阅读:
    可视化svg深入理解viewport、viewbox、preserveaspectradio
    async generator promise异步方案实际运用
    JavaScript中面相对象OOP
    css3:神秘的弹性盒子flexbox
    JavaScript:我总结的数组API
    CSS3:过渡大全
    CSS3奇特的渐变示例
    缓存:前端页面缓存、服务器缓存(依赖SQL)MVC3
    nohup
    video和audio
  • 原文地址:https://www.cnblogs.com/Travis990/p/4502531.html
Copyright © 2011-2022 走看看