1. 给QPushButton 增加背景图片:背景图片可根据Button大小自由缩放。
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");//消除边框,取消点击效果 }
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);
QPixmap pixmap(normalIcon); QPixmap fitpixmap=pixmap.scaled(labelIcon->width(), labelIcon->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); labelIcon->setPixmap(fitpixmap);
lastBtn->setStyleSheet("background-image: url(:/images/btn_previous_normal.png);border: 0px");
原文地址:http://blog.csdn.net/liukang325/article/details/44832397