QT中給各控件增加背景圖片(可縮放可旋轉)的幾種方法


1. 給QPushButton 增加背景圖片:背景圖片可根據Button大小自由縮放。

 

[cpp]  view plain  copy
 
 在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 plain  copy
 
 在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 plain  copy
 
 在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 plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. lastBtn->setStyleSheet("background-image: url(:/images/btn_previous_normal.png);border: 0px");  

 

 

QPixmap旋轉圖片:

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
    1. QMatrix leftmatrix;  
    2. leftmatrix.rotate(270);  
    3. ui->label->setPixmap(pixmap.transformed(leftmatrix,Qt::SmoothTransformation));  

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM