How to set image to the background of the centralWidget?
-
How do i set the picture into the window background without it 'replicating' the image and filling the background with black?
Im resizing the picture and i didn't understand how to change the position where the image will be drawn:
Launcher::Launcher(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); //ui.centralWidget->setObjectName("objectName"); //ui.centralWidget->setStyleSheet("#objectName { background-image: url(:/Launcher/cat) }"); QPixmap bkgnd(":/Launcher/cat"); bkgnd = bkgnd.scaled(QSize(300, 300), Qt::KeepAspectRatio); //bkgnd.fill(Qt::transparent); QPalette palette; palette.setBrush(QPalette::Window, bkgnd); //palette.setBrush(QPalette::Window, Qt::transparent); //this->setAttribute(Qt::WA_TranslucentBackground, true); //this->setStyleSheet("background: transparent;"); //this->setAutoFillBackground(true); this->setPalette(palette); }
Result:
cat.png:
https://i.imgur.com/XPhquk5.jpeg -
@Cesar
I would Googleqt stylesheet background-image
. For example, https://forum.qt.io/topic/40151/solved-scaled-background-image-using-stylesheet may be useful. I see that, like for CSS, the default for a background image is "repeat"/"tile", meaning that a small image is repeatedly tiles to cover the whole area. You need theno-repeat
, at least some parts of:background: url(:/res/background.jpg) no-repeat center center fixed;
I am not sure about the white background shown in your
imgur
link and the black background shown in your Qt. I know that for GIFs you have to "tell it" which color is background in order to achieve transparency. Do you have to do the same thing for a JPEG? Googlingjpeg transparent background
implies to me that this is not easy? One answer at https://qr.ae/pvatpF claims:JPEGs don’t support transparency, but PNGs and GIFs do. If you want the image to have transparent areas it needs to be converted to a PNG or GIF.