您的位置:首页 > 编程语言 > Qt开发

QT中给各控件增加背景图片(可缩放可旋转)的几种方法

2015-11-11 17:36 746 查看
1. 给QPushButton 增加背景图片:背景图片可根据Button大小自由缩放。

[cpp] view
plaincopy





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
plaincopy





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
plaincopy





QPixmap pixmap(normalIcon);

QPixmap fitpixmap=pixmap.scaled(labelIcon->width(), labelIcon->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

labelIcon->setPixmap(fitpixmap);

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

[cpp] view
plaincopy





lastBtn->setStyleSheet("background-image: url(:/images/btn_previous_normal.png);border: 0px");

QPixmap旋转图片:

[cpp] view
plaincopy





QMatrix leftmatrix;

leftmatrix.rotate(270);

ui->label->setPixmap(pixmap.transformed(leftmatrix,Qt::SmoothTransformation));

FROM:http://blog.csdn.net/liukang325/article/details/44832397
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: