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

Add an image to the MainWindow using code?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 2.7k Views 2 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.
  • SGaistS SGaist

    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 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?

    mrjjM 1 Reply Last reply
    0
    • ? A Former User

      @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?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      0
      • mrjjM mrjj

        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 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          1
          • SGaistS SGaist

            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 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.

            mrjjM 1 Reply Last reply
            0
            • ? A Former User

              @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.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                • mrjjM mrjj

                  @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 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.

                  mrjjM 1 Reply Last reply
                  0
                  • ? A Former User

                    @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.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 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
                    0
                    • mrjjM mrjj

                      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 last edited by
                      #12

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

                      mrjjM 1 Reply Last reply
                      0
                      • ? A Former User

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

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 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

                        • Login

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