http://blog.csdn.net/liukang325/article/details/44832397
1. Add background image to Qpushbutton: Background image can be scaled freely according to button size.
[CPP]View Plaincopy
- void Setbuttonbackimage (Qpushbutton *button,qstring image,int Sizew, int sizeh)
- {
- //163,163 is the original resolution, which is slightly adjusted here.
- 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); That's the way it works. Transparent button, useful when using PNG images
- Button->setstylesheet ("border:0px"); Remove border, Cancel click Effect
- }
2. Add background image to Qwidget: The image can be freely scaled.
[CPP]View Plaincopy
- This->setautofillbackground (true); //widget Add background image, this sentence must be.
- Qpixmap Pixmap (":/images/bg_news.png");
- Qpixmap fitpixmap=pixmap.scaled (+) scaled (CONFIG->MAINWINDOWW,CONFIG->MAINWINDOWH, Qt::I Gnoreaspectratio, qt::smoothtransformation);
- Qpalette Palette;
- Palette.setbrush (Qpalette::background, Qbrush (Fitpixmap));
- This->setpalette (palette);
3. Add background image to Qlabel: The image can be freely scaled.
[CPP]View Plaincopy
- Qpixmap Pixmap (Normalicon);
- Qpixmap fitpixmap=pixmap.scaled (Labelicon->width (), Labelicon->height (), Qt::ignoreaspectratio, Qt:: Smoothtransformation);
- Labelicon->setpixmap (FITPIXMAP);
4. Use QSS style, add background image, picture shows original proportions.
[CPP]View Plaincopy
- Lastbtn->setstylesheet ("Background-image:url (:/images/btn_previous_normal.png); border:0px");
Qpixmap Rotate Picture:
[CPP]View Plaincopy
- Qmatrix Leftmatrix;
- Leftmatrix.rotate (270);
- Ui->label->setpixmap (pixmap.transformed (leftmatrix,qt::smoothtransformation));
Several ways to add background images (scalable and rotatable) to each control in QT