Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add a Backrgound Image on my QMainWindow

How to add a Backrgound Image on my QMainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Victor Delattre
    wrote on last edited by
    #1

    Hi! Im new to Qt and Im struggling on how to do this,
    I tried two methods but none of them worked.
    The methods are here:
    864851d1-fa4c-4ae9-bd97-1cf6e50837cb-image.png
    (I don't use them directly in the QMainWindow constructor because I don't want the background to be definitive in my window)
    Thank you for your help!!

    JonBJ 1 Reply Last reply
    0
    • V Victor Delattre

      Hi! Im new to Qt and Im struggling on how to do this,
      I tried two methods but none of them worked.
      The methods are here:
      864851d1-fa4c-4ae9-bd97-1cf6e50837cb-image.png
      (I don't use them directly in the QMainWindow constructor because I don't want the background to be definitive in my window)
      Thank you for your help!!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Victor-Delattre
      Please paste code, not show screenshots.

      For background palette I think you need to set setAutoFillBackground(true) on any widget using it.

      For style sheet, you are using a rule for MainWindow, but you don't have any widget with that name. QMainWindow or MainMenu might work for you as those are your classes.

      V 1 Reply Last reply
      1
      • JonBJ JonB

        @Victor-Delattre
        Please paste code, not show screenshots.

        For background palette I think you need to set setAutoFillBackground(true) on any widget using it.

        For style sheet, you are using a rule for MainWindow, but you don't have any widget with that name. QMainWindow or MainMenu might work for you as those are your classes.

        V Offline
        V Offline
        Victor Delattre
        wrote on last edited by
        #3

        @JonB Hi thank you for your answers!
        I tried them but I still have the problem.
        WIth Palette now it changes the background (was default grey now it's black) but I don't have my background image.
        WIth StyleSheet either with QMainWindow or MainMenu it does nothing.
        Pasting the code here:
        On main:

        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
        
            MainMenu Fenetre_Principale;
            Fenetre_Principale.lancerJeu();
        
            return app.exec();
        }
        

        On MainMenu.cpp:

        MainMenu::MainMenu(QWidget *parent) : QMainWindow(parent)
        {
            setFixedSize(largeur, hauteur);
            setWindowTitle("Badlands Cities");
            setWindowIcon(QIcon(":/Images/icon.jpeg"));
        }
        
        void MainMenu::lancerJeu()
        {
            Bouton_Play = new CustomButton("Jouer", this);
            Bouton_Play->setDefault(true);
            Bouton_Play->move(pour_larg*10, pour_haut*25);
        
            Bouton_Quit = new CustomButton("Quitter", this);
            Bouton_Quit->move(pour_larg*10, pour_haut*50);
        
            //First Method with QPalette:
            /*
            setAutoFillBackground(true);
            QPixmap bkgnd(":/Images/MenuBackground.jpeg");
            bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
            QPalette pal;
            pal.setBrush(QPalette::Window, bkgnd);
            setPalette(pal);
            */
        
            //Second Method with StyleSheet:
            /*
            QString Sheet = R"(
                MainMenu {
                    border-image: url(":/Images/MenuBackground.jpeg") 0 0 0 0 stretch stretch;
            })";
            setStyleSheet(Sheet);
            */
            //Or:
            /*
            QString Sheet = R"(
                background-image: url(":/Images/MenuBackground.jpeg");
                background-position: center;
            )"
            CentralWidget()->setStyleSheet(Sheet);
            */
        
            this->show();
        
            QObject::connect(Bouton_Quit, SIGNAL(clicked()), qApp, SLOT(quit()));
            QObject::connect(Bouton_Play, SIGNAL(clicked()), this, SLOT(lancerMenu()));
        }
        
        V 1 Reply Last reply
        0
        • V Victor Delattre

          @JonB Hi thank you for your answers!
          I tried them but I still have the problem.
          WIth Palette now it changes the background (was default grey now it's black) but I don't have my background image.
          WIth StyleSheet either with QMainWindow or MainMenu it does nothing.
          Pasting the code here:
          On main:

          int main(int argc, char *argv[])
          {
              QApplication app(argc, argv);
          
              MainMenu Fenetre_Principale;
              Fenetre_Principale.lancerJeu();
          
              return app.exec();
          }
          

          On MainMenu.cpp:

          MainMenu::MainMenu(QWidget *parent) : QMainWindow(parent)
          {
              setFixedSize(largeur, hauteur);
              setWindowTitle("Badlands Cities");
              setWindowIcon(QIcon(":/Images/icon.jpeg"));
          }
          
          void MainMenu::lancerJeu()
          {
              Bouton_Play = new CustomButton("Jouer", this);
              Bouton_Play->setDefault(true);
              Bouton_Play->move(pour_larg*10, pour_haut*25);
          
              Bouton_Quit = new CustomButton("Quitter", this);
              Bouton_Quit->move(pour_larg*10, pour_haut*50);
          
              //First Method with QPalette:
              /*
              setAutoFillBackground(true);
              QPixmap bkgnd(":/Images/MenuBackground.jpeg");
              bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
              QPalette pal;
              pal.setBrush(QPalette::Window, bkgnd);
              setPalette(pal);
              */
          
              //Second Method with StyleSheet:
              /*
              QString Sheet = R"(
                  MainMenu {
                      border-image: url(":/Images/MenuBackground.jpeg") 0 0 0 0 stretch stretch;
              })";
              setStyleSheet(Sheet);
              */
              //Or:
              /*
              QString Sheet = R"(
                  background-image: url(":/Images/MenuBackground.jpeg");
                  background-position: center;
              )"
              CentralWidget()->setStyleSheet(Sheet);
              */
          
              this->show();
          
              QObject::connect(Bouton_Quit, SIGNAL(clicked()), qApp, SLOT(quit()));
              QObject::connect(Bouton_Play, SIGNAL(clicked()), this, SLOT(lancerMenu()));
          }
          
          V Offline
          V Offline
          Victor Delattre
          wrote on last edited by
          #4

          @Victor-Delattre Can someone help me?
          All I want is to use an image on the background of my window !

          J.HilkJ 1 Reply Last reply
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #5

            Does you app correctly show the window icon you've set?
            I feel like it didn't load the jpeg picture successfully.

            After

            QPixmap bkgnd(":/Images/MenuBackground.jpeg");
            

            check the value of bkgnd.isNull().

            V 2 Replies Last reply
            0
            • V Victor Delattre

              @Victor-Delattre Can someone help me?
              All I want is to use an image on the background of my window !

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @Victor-Delattre the palette version works just fine for me

              7e5015d1-6108-4bd6-acc6-7775f5da20ac-image.png


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • B Bonnie

                Does you app correctly show the window icon you've set?
                I feel like it didn't load the jpeg picture successfully.

                After

                QPixmap bkgnd(":/Images/MenuBackground.jpeg");
                

                check the value of bkgnd.isNull().

                V Offline
                V Offline
                Victor Delattre
                wrote on last edited by
                #7

                @Bonnie
                Yes my Icon is correctly displayed,
                I don't know how to print the bkgnd.isNull() value but I understand what you were talking about.
                What do I have to do to get access to this value in Qt Creator?
                Btw I will try to change the image that is loaded, maybe it doesn't fit with Qt.
                Thanks for your answer !

                1 Reply Last reply
                0
                • B Bonnie

                  Does you app correctly show the window icon you've set?
                  I feel like it didn't load the jpeg picture successfully.

                  After

                  QPixmap bkgnd(":/Images/MenuBackground.jpeg");
                  

                  check the value of bkgnd.isNull().

                  V Offline
                  V Offline
                  Victor Delattre
                  wrote on last edited by
                  #8

                  @Bonnie Alright!
                  When I change my Background Image it works!
                  Thanks for your answers all!

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved