Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Add an image to the MainWindow using code?



  • 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!


  • Lifetime Qt Champion

    Hi,

    You are looking for QLabel.

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


  • Lifetime Qt Champion

    Hi,

    You are looking for QLabel.

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



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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

    You can use setCentralWidget on your label.



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


  • Lifetime Qt Champion

    @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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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


Log in to reply