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
[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
相关文章推荐
- Qt中图像的显示与基本操作
- [Qt]一个简单的Qt Widget多线程处理图像的例子
- Qt继电器控制板代码
- 在QT中使用静态对象
- Qt下如何使用libuv
- Qt学习点滴积累
- QT位置相关函数
- 自学QT之底层因为接收到操作系统信号而停止的解决方案
- Qt Creator的下载、安装及试用
- QT主窗口QMainWindow
- VS调试Qt程序时查看变量显示地址值问题解决
- qt 中各种常用对话框
- QML Item详解
- Qt数据库sqlite总结
- Qt中提高sqlite的读写速度
- Qt 的sqlite数据库的学习
- Mobile Assistant
- Home Server
- Qt应用程序图标的个性化设置
- QTime获取当前时间,QDateTime获取日期和时间,QTimer定时器,QLCDNumber数码显示框