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. Adjust an image on MainWindow (directory problems)
Qt 6.11 is out! See what's new in the release blog

Adjust an image on MainWindow (directory problems)

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 4.3k Views 1 Watching
  • 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.
  • jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #3

    "I stored my image in the same folder of my project" - the file should be in the same directory as your executable.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    2
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #4

      Hi
      In most cases, you would add a qt resource file and add the image to that.
      then load using the special syntax ":/"
      http://www.bogotobogo.com/Qt/Qt5_Resource_Files.php

      In your case, you ask it to load without any path so it will look where exe file is
      or what ever is current directory.

      If you dont want to use res file. you can use

      qDebug() << "App path : " << qApp->applicationDirPath();

      That is where the exe is generated. the build folder or later the install folder.
      so
      QBrush b(Pixmap(qApp->applicationDirPath()+"/"+ "fondo.jpg"));
      palette->setBrush(QPalette::Background, b);

      (ps. you do not need to new brushes or pixmaps most of the times);

      psps. put image in build folder with exe .

      1 Reply Last reply
      1
      • yczoY Offline
        yczoY Offline
        yczo
        wrote on last edited by yczo
        #5

        I will try the yout solutions thank you very much.

        I was talking about the vertical tho points " :" type ":\project\archive"
        Greetings.

        mrjjM 1 Reply Last reply
        0
        • yczoY yczo

          I will try the yout solutions thank you very much.

          I was talking about the vertical tho points " :" type ":\project\archive"
          Greetings.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #6

          @yczo

          • ":/"
            is syntax for loading from resource file

          http://doc.qt.io/qt-5/resources.html

          1 Reply Last reply
          0
          • yczoY Offline
            yczoY Offline
            yczo
            wrote on last edited by
            #7

            I don't know what happens, the image was once show, but no more
            :-(

            mrjjM 1 Reply Last reply
            0
            • yczoY yczo

              I don't know what happens, the image was once show, but no more
              :-(

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @yczo said:

              setCentralWidget

              Hi, maybe you just put widgets over it ?

              1 Reply Last reply
              0
              • yczoY Offline
                yczoY Offline
                yczo
                wrote on last edited by
                #9

                Here is my new code, but do not work, show a black window
                fondo.jpg is in the same path like the project directory

                MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {

                    //QPalette* palette = new QPalette();
                    QPalette palette;
                    palette.setBrush(QPalette::Background,QBrush(QPixmap("fondo.jpg")));
                    setPalette(palette);
                    ui->setupUi(this);
                    setFixedSize(1333,768);
                

                }

                1 Reply Last reply
                0
                • yczoY Offline
                  yczoY Offline
                  yczo
                  wrote on last edited by
                  #10

                  please, a tutorial on the use of images and icons in Qt?

                  1 Reply Last reply
                  0
                  • yczoY Offline
                    yczoY Offline
                    yczo
                    wrote on last edited by
                    #11

                    The image should not be in the project folder, but the object of compilation, in the root, not in debug or relase

                    1 Reply Last reply
                    0
                    • jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      You should first call ui->setupUi(this); and then do all the other UI related stuff:

                      MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                          //QPalette* palette = new QPalette();
                          QPalette palette;
                          palette.setBrush(QPalette::Background,QBrush(QPixmap("fondo.jpg")));
                          setPalette(palette);
                          setFixedSize(1333,768);
                      }
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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