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)
Forum Updated to NodeBB v4.3 + New Features

Adjust an image on MainWindow (directory problems)

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 3.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.
  • yczoY Offline
    yczoY Offline
    yczo
    wrote on last edited by
    #1

    Hello,
    I try to adjust an image in the background of Mainwindow, below is my code, I think that it's right, (another better suggestion will be wellcome)... but the image don't show. Could be a path problem? I don't understand what the two points in a path mean. I stored my image in the same folder of my project

    thanks in advance
    Greetings

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QGridLayout>
    #include <QPalette>
    #include <QPushButton>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
    
            QPalette* palette = new QPalette();
            palette->setBrush(QPalette::Background,*(new QBrush(*(new QPixmap("fondo.jpg")))));
            setPalette(*palette);
    
         //   setWindowFlags(Qt::FramelessWindowHint); //sirve para poner ventana sin botones
            QWidget *centralWidget = new QWidget(this);
            QGridLayout *layout = new QGridLayout();
    
            centralWidget->setLayout(layout);
            layout->addWidget(new QPushButton("Button 1"),0,0);
            layout->addWidget(new QPushButton("Button 2"),0,1);
            layout->addWidget(new QPushButton("Button 3"),0,2);
            setCentralWidget(centralWidget); //Prueba 1 funciona regular
            ui->setupUi(this);
            setFixedSize(1333,768);
    
    }
    
    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why all these pointers?!
      Why not just:

      QPalette palette;
      palette.setBrush(QPalette::Background,QBrush(QPixmap("fondo.jpg")));
      

      You should check whether this QPixmap("fondo.jpg") creates a valid QPixmap. From documentation:
      "Constructs a pixmap from the file with the given fileName. If the file does not exist or is of an unknown format, the pixmap becomes a null pixmap."

      Two points in a path means parent directory:

      c:\first\second\..\file.txt
      means (is same as)
      c:\first\file.txt
      

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

      1 Reply Last reply
      2
      • 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