Qt setstylesheet Add a background color/background image (using a non-resource image)

Source: Internet
Author: User

Easy to fix, MainWindow is a qwidget.
Set the background color to blue
Mainwindow.setstylesheet ("background-color:blue;");

Set the background image to Bg.png
Mainwindow.setobjectname ("MainWindow");
Mainwindow.setstylesheet ("#mainWindow {Border-image:url (:/image/bg.png);}");

Note here that ObjectName is not optional and will cause all child controls of the widget to have the same background if not used. The reason for not using "background-image" is that "border-image" can be automatically populated.

If you want to restore the default background, you can call
Mainwindow.setstylesheet (0);

:/image/bg.png preceded by a colon stating that it is a resource defined in QRC; by the way, how to add and edit QRC files.
On the project right click on "Add New ..." and then "qt" = "qt Resource file", a random name = "Next" + "Finish", add QRC file complete.
Next add the picture resource, select the QRC file that you just added, "Add Prefix", suggest "Prefix" set to "/"; then "Add Files" to add the picture to the resource file.

Of course, you can also load pictures directly from the disk. Compared to using a picture defined in a resource file, the time overhead is large, but the space overhead is small and more flexible, such as displaying downloaded pictures.
Mainwindow.setobjectname ("MainWindow");
Mainwindow.setstylesheet ("#mainWindow {border-image:url (c:/downloads/myapp/image/bg.png);}");

http://blog.csdn.net/itjobtxq/article/details/9284415

Qt setstylesheet Add a background color/background image (using a non-resource image)

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.