QT中給各控件增加背景圖片(可縮放可旋轉)的幾種方法
阿新 • • 發佈:2017-10-20
.net detail eight iou rotate art board 按鈕 previous
1. 給QPushButton 增加背景圖片:背景圖片可根據Button大小自由縮放。
[cpp] view plain copy
- void setButtonBackImage(QPushButton *button,QString image,int sizeW, int sizeH)
- {
- //163,163為原始分辨率,這裏稍做了調整。
- QPixmap pixmap(image);
- QPixmap fitpixmap=pixmap.scaled(163,163).scaled(sizeW, sizeH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
- button->setIcon(QIcon(fitpixmap));
- button->setIconSize(QSize(sizeW,sizeH));
- button->setFlat(true);//就是這句能夠實現按鈕透明,用png圖片時很有用
- button->setStyleSheet("border: 0px");//消除邊框,取消點擊效果
- }
2. 給QWidget 增加背景圖片:圖片可自由縮放。
[cpp] view plain copy
- this->setAutoFillBackground(true); //Widget增加背景圖片時,這句一定要。
- QPixmap pixmap(":/images/bg_news.png");
- QPixmap fitpixmap=pixmap.scaled(1200, 1200).scaled(config->mainWindowW,config->mainWindowH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
- QPalette palette;
- palette.setBrush(QPalette::Background, QBrush(fitpixmap));
- this->setPalette(palette);
3. 給QLabel 增加背景圖片:圖片可自由縮放。
[cpp] view plain copy
- QPixmap pixmap(normalIcon);
- QPixmap fitpixmap=pixmap.scaled(labelIcon->width(), labelIcon->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
- labelIcon->setPixmap(fitpixmap);
4. 采用QSS樣式,增加背景圖片,圖片顯示原始比例。
[cpp] view plain copy
- lastBtn->setStyleSheet("background-image: url(:/images/btn_previous_normal.png);border: 0px");
QPixmap旋轉圖片:
[cpp] view plain copy- QMatrix leftmatrix;
- leftmatrix.rotate(270);
- ui->label->setPixmap(pixmap.transformed(leftmatrix,Qt::SmoothTransformation));
http://blog.csdn.net/liukang325/article/details/44832397
QT中給各控件增加背景圖片(可縮放可旋轉)的幾種方法