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. Can't display a png
Forum Updated to NodeBB v4.3 + New Features

Can't display a png

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.6k 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.
  • R Offline
    R Offline
    rvollemaere
    wrote on last edited by
    #1

    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) :
    QMainWindow(parent)
    {
    QVBoxLayout *layout = new QVBoxLayout();;
    QLabel *labelImage = new QLabel();

    labelImage->setPixmap(QPixmap("logoCTD.png"));
    layout->addWidget(labelImage);
    setLayout(layout);
    

    }

    MainWindow::~MainWindow()
    {

    }@

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.
      1 Reply Last reply
      0
      • G Offline
        G Offline
        Guigui
        wrote on last edited by
        #3

        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();
        labelImage->setPixmap(QPixmap("lena.png"));
        setCentralWidget(labelImage);
        }@

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

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rvollemaere
          wrote on last edited by
          #4

          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!

          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