Home qt How to add to the background wallpaper

How to add to the background wallpaper

Author

Date

Category

To add to the background wallpaper?
I want to add to the background image in the window .png
mainWindow and dialog. How to do it?

Add the code in styleSheet.
QWidget {
background-image: url ( “/ image / layer.png”) no-repeat right bottom;
}

The background should be in the background and then it turns out that on each lable and graphicsview it appears.


Answer 1, Authority 100%

Prescribe stylesheet. the image must be placed in the resource

Example:

I had to place a background logo in the bottom right corner of the widget tree.
in this case it was necessary to fix the position of the background does not move when scrolling.
the editor asked the stylesheet:

QTreeView {
background: url (: / ico / background) no-repeat right bottom;
 Background-Size: Cover;
background-attachment: fixed;
border-bottom-width: 5;
border-right-width: 15;
}

it so:

Additionally.

Keep in mind that the styles are inherited to the descendants (considering the object of inheritance). Therefore, in your example, the style for QWidget will apply to all descendants (because they are heirs QWidget). To avoid this, ask for specific styles of widgets and not for the entire window, or specify a particular class: QTreeWidget, QFrame, etc., or use selectors. For example:

QWidget [objectName = "centralWidget"] {background-image: url ( ": / img / bg") no-repeat right bottom; }
QWidget [objectName = "myFrame"] {background-color: # ff0000; }
QLineEdit {background-color: # 00ff00; }
QWidget [enabled = "false"] {background-color: # 0000ff; }

Answer 2

If anyone should be a solution:

QPixmap bkgnd ( "./ image / layer.png");
  bkgnd = bkgnd.scaled (size (), Qt :: IgnoreAspectRatio);
  QPalette p = palette ();
  p.setBrush (QPalette :: Background, bkgnd);
  setPalette (p);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions