zeboidlund
zeboidlund

Reputation: 10147

QT .png File not showing up on screen

All I appear to get is a white screen when I compile and run my project. The image does exist, and the images folder is in my project's root directory. All I'm trying to do is load a simple image onto the screen. Is there anything specific I'm missing here?

Code

#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QDir>
#include "chronos_main_window.h"

int main(int argc, char *argv[])
{
    QDir dir;
    QApplication a(argc, argv);
    QGraphicsScene scene;
    //ChronosMainWindow window;
    QGraphicsView view(&scene);
    QGraphicsPixmapItem item(QPixmap(dir.relativeFilePath("images\\ozone_sprite.png"))); //images folder is located in project root dir
    scene.addItem(&item);
    //window.show();
    view.show();

    return a.exec();
}

Upvotes: 0

Views: 2852

Answers (3)

Andrii
Andrii

Reputation: 1906

I'm not sure, but try declare QDir object after creation instance of QApplication

Upvotes: 0

user999040
user999040

Reputation:

Try this:

in your code:

QGraphicsPixmapItem item(
                     QPixmap(dir.relativeFilePath(":/images/ozone_sprite.png")));

In resource.qrc(if you don't have it, then right click on your project. Add new.. -> Qt -> Qt Resource file), then open it with plain text editor:

<RCC>
<qresource>
    <file>images/ozone_sprite.png</file>
</qresource>
</RCC>

Hope that helps

Upvotes: 1

andrea.marangoni
andrea.marangoni

Reputation: 1499

you can load a QPixmap from the same directory of the application

QPixmap("./image.png")

else you can load the image from a qrc file:

QPixmap(":path") path depends on how you made the qrc file it s difficult to me explaining how to do qrc file because i m not english. but you can find a lot of documentation on internet.

Upvotes: 1

Related Questions