Several ways to add background images (scalable and rotatable) to each control in QT

Source: Internet
Author: User

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
  1. void Setbuttonbackimage (Qpushbutton *button,qstring image,int Sizew, int sizeh)
  2. {
  3. //163,163 is the original resolution, which is slightly adjusted here.
  4. Qpixmap pixmap (image);
  5. Qpixmap fitpixmap=pixmap.scaled (163,163). Scaled (Sizew, Sizeh, Qt::ignoreaspectratio, qt::smoothtransformation);
  6. Button->seticon (Qicon (Fitpixmap));
  7. Button->seticonsize (Qsize (Sizew,sizeh));
  8. Button->setflat (true); That's the way it works. Transparent button, useful when using PNG images
  9. Button->setstylesheet ("border:0px"); Remove border, Cancel click Effect
  10. }


2. Add background image to Qwidget: The image can be freely scaled.

[CPP]View Plaincopy
    1. This->setautofillbackground (true);  //widget Add background image, this sentence must be.
    2. Qpixmap Pixmap (":/images/bg_news.png");
    3. Qpixmap fitpixmap=pixmap.scaled (+) scaled (CONFIG->MAINWINDOWW,CONFIG->MAINWINDOWH, Qt::I Gnoreaspectratio, qt::smoothtransformation);
    4. Qpalette Palette;
    5. Palette.setbrush (Qpalette::background, Qbrush (Fitpixmap));
    6. This->setpalette (palette);


3. Add background image to Qlabel: The image can be freely scaled.

[CPP]View Plaincopy
    1. Qpixmap Pixmap (Normalicon);
    2. Qpixmap fitpixmap=pixmap.scaled (Labelicon->width (), Labelicon->height (), Qt::ignoreaspectratio, Qt:: Smoothtransformation);
    3. Labelicon->setpixmap (FITPIXMAP);

4. Use QSS style, add background image, picture shows original proportions.

[CPP]View Plaincopy
    1. Lastbtn->setstylesheet ("Background-image:url (:/images/btn_previous_normal.png); border:0px");

Qpixmap Rotate Picture:

[CPP]View Plaincopy
      1. Qmatrix Leftmatrix;
      2. Leftmatrix.rotate (270);
      3. Ui->label->setpixmap (pixmap.transformed (leftmatrix,qt::smoothtransformation));

Several ways to add background images (scalable and rotatable) 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.