(SOLVED) Background image Main window



  • Hi there!

    I am having difficulty setting up a background image for the main window. I have managed to do it before but now it just won't work.

    i have searched the internet and the forums for a solution and they are what i have done before, so thats why i am asking for a little help.

    i created a qt resource file and put the image in there.
    i know the URL of the image is correct because the image shows in the .ui designer file when i put it in the stylesheet. but when i run the application it doesnt show the image. only a blank application.

    the application is blank because i specifically created a test project to be sure there are no other interuptions.

    i cleaned the stylesheet of the ui designer file and now i am trying to do it in the code itself (which i managed before succesfully)

    the code is:

    mainwindow.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H@

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    setStyleSheet("MainWindow {background-image:url(:/files/MainPic/Technology-Banner1.jpg)}");
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@

    main.cpp
    @#include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }@

    oke, so just to clarify:

    i tried adding the stylesheet to the centralwidget QWidget that is allready by default in the .ui file
    i tried doing the same to the mainwindow

    and now i am trying through code (which i previously managed)

    and i know the stylesheet code is valid because the image shows in the .ui file when i add it. So just to be completely sure, i copied the stylesheet line directly from the .ui file stylesheet properties.

    i really hope you guys can help me

    thnx!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    With that style sheet you are setting the background of your MainWindow, not the central widget.

    Try with

    @ centralWidget()->setStyleSheet("{background-image:url(:/files/MainPic/Technology-Banner1.jpg)}");@

    Hope it helps



  • Hi SGaist,

    thank you for the reply, but i managed to fix the problem by wiping Windows 8.1 and going back to windows 7. Seems like windows8.1 had some bugs in it because now it works like its supposed to.

    Thnx for the welcome ^^


  • Lifetime Qt Champion

    So the exact same code works with Win7 and not Win8.1 ?



  • that would seem to be the case.. probably my computer had a big bug or something.. because i took the same steps (and not to forget, this is not the first time i am doing this).

    although the mistake may have been something else than windows 8.1.. But reinstalling windows 7 fixxed the problem, whatever the problem was. :P


  • Lifetime Qt Champion

    Win 8.1 might be doing something new that needs to be handled differently by Qt. If you happen to verify your code again against both OS, please consider opening a bug report.



  • Just to clarify Dragann, I take it you made the change suggested by SGaist?

    Would you mind re-posting the code that works on Windows 7, just the mainwindow.cpp part should do it, so I can try it on my Windows 8.1.

    Playing around with this, I found this alternative approach works (modify the url to suit of course):

    @qApp->setStyleSheet("#centralWidget {background-image: url(:/new/prefix1/Blues.png);}");@



  • SGaist, if i happen to verify it again i will let you know.

    ray4qt, i used the same code as i posted above:

    @ setStyleSheet("MainWindow {background-image:url(:/files/MainPic/Technology-Banner1.jpg)}");
    }@

    something was probably just bugging out. When i installed new windows, managed to do what i wanted in a matter of seconds.

    add qrc
    add line of code and BAM it works..

    while before it took me 3-4 hours of troubleshooting,, and then i decided to wipe OS lol :P



  • That works on my Windows 8.1.

    As SGaist points out, it sets the background for all the MainWindow, including the menu and status bars, which is fine if those widgets are not being used.

    But if they are used the image can be confined to the centralWidget like this:

    @ setStyleSheet("#centralWidget {background-image:url(:/files/MainPic/Technology-Banner1.jpg)}");@



  • Yes i know :D,, excuse me for the late reply by the way...

    anyways, something was just bugging out.. because even if i added the image in the ui designer, it would show it in the designer... but as soon as i compiled and ran the program, the background would be gone...

    which is really weird, because if the designer shows the background image correctly, then that means the url and everything is correct...

    and even after that, i run the program and the background is just gone... like some kind of dark magic


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.