⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Can't display a png

  • Hi! I'm new here and I can't display a png with this code and I don't know why...
    It show me an empty window. I tried a lot of manipulation, but I can't find any solution.

    @#include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QVBoxLayout *layout = new QVBoxLayout();;
    QLabel *labelImage = new QLabel();





  • Moderators

    Hi and welcome to devnet.

    There are couple of things that could be wrong here.

    • If it's the automatically generated Qt MainWindow class there should be ui->setupUi call somewhere in there.
    • QMainWindow already has a layout for things like docks, menus, status bar etc. so you shouldn't be replacing it. If you just want to show your label try "QMainWindow::setCentralWidget":http://qt-project.org/doc/qt-5.0/qtwidgets/qmainwindow.html#setCentralWidget
    • Where is the image located? Since you use a relative path it should be in your working directory which might be different from your project or your executable location, depending on how are you running your app. You can check if the image was actually loaded with "QPixmap::isNull":http://qt-project.org/doc/qt-5.0/qtgui/qpixmap.html#isNull
    • Again - depending on how are you running your app it might be missing an image plugin in the imageformats sub-directory. Check the "deployment instructions":http://qt-project.org/doc/qt-5.0/qtdoc/deployment.html for your platform.

  • Well, Chris just posted the answer while I was testing the code, but here's mine anyway. You should see a warning when you execute the code telling you that a QMainWindow already has a layout. If you simply want to display a picture, you could try the following code:

    @MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    QLabel *labelImage = new QLabel();

    If it doesn't work, check if the path to your image is correct.

  • Thank's guys. It was a path issue but I don't really know why it works now.
    I tried with the full path and it worked. Then I try again with the relative path (ctrl+z) and it works.
    So, maybe it was a bug... very strange...

    Thank's again!

Log in to reply