Several methods of adding background images to each control in QT

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.