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. Add an image to the MainWindow using code?
Forum Update on Monday, May 27th 2025

Add an image to the MainWindow using code?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 2.7k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 14 Jul 2021, 18:42 last edited by
    #1

    Hello everybody, hope all is well!

    Currently, I am trying to figure out how to add either a QImage or a QPixmap widget to a window in the source code. I am not using any UI design files (form files), as I want to do this without using them. Thus, I am trying to figure out how to do so through code. As of now, this is what I have done:

    QImage *img = new QImage("image.file");
    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(img);
    

    However, I have had no luck with this. What do I need to do to insert an image (as well as place it in a precise location)? Thank you in advance!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Jul 2021, 18:44 last edited by
      #2

      Hi,

      You are looking for QLabel.

      Do not allocated QImage on the heap. There's no need for that.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      ? 1 Reply Last reply 14 Jul 2021, 20:01
      0
      • S SGaist
        14 Jul 2021, 18:44

        Hi,

        You are looking for QLabel.

        Do not allocated QImage on the heap. There's no need for that.

        ? Offline
        ? Offline
        A Former User
        wrote on 14 Jul 2021, 20:01 last edited by
        #3

        @SGaist Thank you! Would this work?

        QPixmap *img = new QPixmap("image.file");
        QLabel *label = new QLabel();
        label->setPixmap(img);
        QVBoxLayout *layout = new QVBoxLayout();
        layout->addWidget(label);
        

        Or would I need to try something else?

        M 1 Reply Last reply 14 Jul 2021, 20:02
        0
        • ? A Former User
          14 Jul 2021, 20:01

          @SGaist Thank you! Would this work?

          QPixmap *img = new QPixmap("image.file");
          QLabel *label = new QLabel();
          label->setPixmap(img);
          QVBoxLayout *layout = new QVBoxLayout();
          layout->addWidget(label);
          

          Or would I need to try something else?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 14 Jul 2021, 20:02 last edited by mrjj
          #4

          Hi
          Yes but no need to new the pixmap

          QPixmap img ("image.file");
          QLabel *label = new QLabel();
          label->setPixmap(img);
          QVBoxLayout *layout = new QVBoxLayout();
          layout->addWidget(label);
          

          Do note that using no path to the image wont work well if you have the image in the project folder as when exe runs, the current folder is the build folder so it looks there.

          ? 1 Reply Last reply 14 Jul 2021, 20:15
          0
          • M mrjj
            14 Jul 2021, 20:02

            Hi
            Yes but no need to new the pixmap

            QPixmap img ("image.file");
            QLabel *label = new QLabel();
            label->setPixmap(img);
            QVBoxLayout *layout = new QVBoxLayout();
            layout->addWidget(label);
            

            Do note that using no path to the image wont work well if you have the image in the project folder as when exe runs, the current folder is the build folder so it looks there.

            ? Offline
            ? Offline
            A Former User
            wrote on 14 Jul 2021, 20:15 last edited by
            #5

            @mrjj Thank you.
            Here is my code now for the MainWindow Class

            MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
            {
                QPixmap img (":images/appLogo.svg");
                QLabel *label = new QLabel();
                label->setPixmap(img);
                QVBoxLayout *layout = new QVBoxLayout();
                layout->addWidget(label);
                setLayout(layout);
            }
            

            However, the window is still blank. I tried the adding in the setLayout function, but still had no luck. Is there an addLabel function similar to the addDockWidget function or would I need to do something else?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 14 Jul 2021, 20:18 last edited by
              #6

              QMainWindow already has a layout. The one that provides all the nice feature like docking, etc.

              You can use setCentralWidget on your label.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              ? 1 Reply Last reply 14 Jul 2021, 20:28
              1
              • S SGaist
                14 Jul 2021, 20:18

                QMainWindow already has a layout. The one that provides all the nice feature like docking, etc.

                You can use setCentralWidget on your label.

                ? Offline
                ? Offline
                A Former User
                wrote on 14 Jul 2021, 20:28 last edited by
                #7

                @SGaist Thank you, this helped me display the image itself. What do I need to do if I only want the image on the left side of the window? It is the app's logo, and I want to have buttons/text boxes to the right.

                M 1 Reply Last reply 14 Jul 2021, 20:39
                0
                • ? A Former User
                  14 Jul 2021, 20:28

                  @SGaist Thank you, this helped me display the image itself. What do I need to do if I only want the image on the left side of the window? It is the app's logo, and I want to have buttons/text boxes to the right.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 14 Jul 2021, 20:39 last edited by
                  #8

                  @WesLow
                  Hi
                  Is there a reason you are not using UI files ?

                  You need to add layout to central widget then add some layouts to that to divide it into sections and then place the LOGO label etc,

                  alt text

                  ? 1 Reply Last reply 14 Jul 2021, 20:50
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 14 Jul 2021, 20:40 last edited by
                    #9

                    You can create a QWidget subclass and reimplement the paintEvent method to draw the image where you want it.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • M mrjj
                      14 Jul 2021, 20:39

                      @WesLow
                      Hi
                      Is there a reason you are not using UI files ?

                      You need to add layout to central widget then add some layouts to that to divide it into sections and then place the LOGO label etc,

                      alt text

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on 14 Jul 2021, 20:50 last edited by
                      #10

                      @mrjj Thank you. Yes, it's a weird requirement from my teacher. UI files would make this much easier haha. Also thank you for that example, my thinking is kind of similar to yours there.

                      @SGaist Is there a good example of this on QT's website; I'm not really sure what you mean by what you're saying.

                      Thank you both for your help, it is very much appreciated.

                      M 1 Reply Last reply 14 Jul 2021, 21:05
                      0
                      • ? A Former User
                        14 Jul 2021, 20:50

                        @mrjj Thank you. Yes, it's a weird requirement from my teacher. UI files would make this much easier haha. Also thank you for that example, my thinking is kind of similar to yours there.

                        @SGaist Is there a good example of this on QT's website; I'm not really sure what you mean by what you're saying.

                        Thank you both for your help, it is very much appreciated.

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 14 Jul 2021, 21:05 last edited by
                        #11

                        Hi

                        • Yes, it's a weird requirement from my teacher.

                        Ok... His class his rules. .. but you can cheat a bit if you want. Using UI files just generate
                        code. So you can draw your layout and then steal the code from the setupUI function
                        or at least be inspired.

                        ? 1 Reply Last reply 14 Jul 2021, 21:20
                        0
                        • M mrjj
                          14 Jul 2021, 21:05

                          Hi

                          • Yes, it's a weird requirement from my teacher.

                          Ok... His class his rules. .. but you can cheat a bit if you want. Using UI files just generate
                          code. So you can draw your layout and then steal the code from the setupUI function
                          or at least be inspired.

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on 14 Jul 2021, 21:20 last edited by
                          #12

                          @mrjj Awesome, I can't seem to find the setupUI function though. Where would that be located?

                          M 1 Reply Last reply 15 Jul 2021, 06:37
                          0
                          • ? A Former User
                            14 Jul 2021, 21:20

                            @mrjj Awesome, I can't seem to find the setupUI function though. Where would that be located?

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 15 Jul 2021, 06:37 last edited by
                            #13

                            @WesLow
                            well make a new default gui project using the wizard

                            Then its in MainWindow

                            MainWindow::MainWindow(QWidget* parent) :
                            QMainWindow(parent),
                            ui(new Ui::MainWindow) {
                            ui->setupUi(this); <<<<

                            You can also run tool directly on a UI file
                            https://doc.qt.io/qt-5/uic.html

                            but use a default project is most likely easier.

                            1 Reply Last reply
                            0

                            1/13

                            14 Jul 2021, 18:42

                            • Login

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