1. Add background image to Qpushbutton: Background image can be scaled freely according to button size.
void Setbuttonbackimage (Qpushbutton *button,qstring image,int sizew, int sizeh) { //163,163 is the original resolution, slightly tuned 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);//Is this sentence can achieve button transparency, with PNG image is useful button->setstylesheet ("border:0px");//clear border, cancel click Effect}
2. Add background image to Qwidget: The image can be freely scaled.
This->setautofillbackground (true); When the widget adds a 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.
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.
Lastbtn->setstylesheet ("Background-image:url (:/images/btn_previous_normal.png); border:0px");
Several methods of adding background images to each control in QT