Nominate our 2022 Qt Champions!

how to display a background image onto the main window?

  • Hi All
    how to display a background image onto the main window? The code that I have currently does not seem to be working as the following:
    this->setStyleSheet("background-image: url(:/res/image1.png);");
    I am trying to retrieve the image file from the resource folder that I have created. So, what is listed is resources -> resources.qrc -> /res -> image1.png.

  • Moderators

    this is the main window?
    Probably you need to set a transparent background on the main windows central widget or even set background image directly on it's central widget instead.

  • @raven-worx
    I have tried this:
    ui->centralWidget->setStyleSheet("background-image: url(:res/image1.png);");
    this also:
    ui->centralWidget->setStyleSheet("background-image: url(:/image1.png);");
    this also:
    ui->centralWidget->setStyleSheet("background-image: url(:/Resources//res/image1.png);");

    No Go! It seem that I am not configuring path correctly?

  • Moderators

    url(:/res/image1.png) should be ok regarding the info you provided.
    But to be sure you can post the qrc.

    Also you can check if QFile(":/res/image1.png")::exists() returns true.

    Also its' problematic to set the stylesheet like you are doing, since it will get applied to all (child-)widgets.
    You should rather limit it with a selector:

    myMainWindoer->setCentralWidget( centralWidget );
    centralWidget ->setStylesheet( "#centralWidget { background-image: url(:/res/image1.png); }" );

  • @raven-worx
    Thank you for the information. I will work upon it.

Log in to reply