Background image stretch and opacity not working
-
I'm trying to style my app a little bit. So I added a background image and I want it to be stretchy with no repeat and with a little opacity. I'm using this code:
this->centralWidget()->setStyleSheet("background-image: url(:/image.jpg) 0 0 0 0; stretch stretch; opacity: 50; background-repeat: no-repeat; background-position: center;");
But the opacity and stretching seems not to work.
-
hi @Hmmm and welcome
take a look at this old thread
https://forum.qt.io/topic/40151/solved-scaled-background-image-using-stylesheet/3I don't think much changed, so using border-image might do the trick for you
-
@J.Hilk Thank you for your answer. It did solve stretching problem but not opacity. Also it added the same image to my buttons. I tried solving it with:
ui->myButton->setStyleSheet("background-color: white; ");
But it doesn't work, image is still there.
-
If you dont define an object / class where to set the stylesheet, it will use the current one and all of its childs... So when you set the bg-image to QMainWindow without using
("QMainWindow { border-image: img.jpg }")
... it will apply the stylesheet (background, in your case) to all valid child widgets from your QMainWindow. Every MessageBox, every PushButton, every Label... has the same background as QMainWindow then.
EDIT:
Ofc you have to use the correct class (+ object name) for your central widget, if you want to set the BG to your CentralWidget only (not whole MainWindow BG) and there are other types of widgets inside your MainWindow (Menus, ToolBars...etc)