jason dancks
jason dancks

Reputation: 1142

First time using Qt: How to display an image?

Noob: How to display an image I am very new to this, just starting actually. I need to figure out how to display an image on screen.

First I tried:

Source code:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    QPixmap qp = QPixmap("../images/tank2.bmp");
    if(qp.isNull())
    {
        printf("Yes its null\n");
    }
    else
    {
        QGraphicsPixmapItem item(QPixmap("../images/tank2.bmp"));
        scene.addItem(&item);
    }
    view.show();
    return a.exec();

}

from:

Qt jpg image display

it compiles and runs but doesn't show an image. returns 0, etc.

Then I just sorta messed around from there. I'm also curious about something: In Qt editor they show a file structure that doesn't exist on the disk. They have files "Headers", "Sources", and Resources whereas on the systems its just a folder "projectname" with all the files in one folder. Is this just for visual clarity?

The version of QtCreator I'm using is 2.4.1 running Qt 4.7.4 64 bit.

My eventual goal is to make a widget where a picture is a clickable icon where you select that pic and can place it on a larger screen like a tile.

Another Question: Why does Qt have things like "QString" and "QChar"? Is there something wrong with the normal c++ libraries?

Upvotes: 2

Views: 14180

Answers (3)

Rustam Safin
Rustam Safin

Reputation: 842

Tested like this, it works. Don't forget to create qrc file.

#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"
#include <stdio.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    QPixmap qp = QPixmap(":/images/123.bmp");
    if(qp.isNull())
    {
        printf("Yes its null\n");
    }
    else
    {
        printf("HAHA");
        QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap(":/images/123.bmp"));
        scene.addItem(item);
    }
    view.show();
    return a.exec();

}

Here are qrc file:

<RCC>
    <qresource prefix="/">
        <file>images/123.bmp</file>
    </qresource>
</RCC>

And .pro file

QT       += core gui
TARGET = testimage
TEMPLATE = app
SOURCES += main.cpp
RESOURCES += 123.qrc

Upvotes: 2

Martin Beckett
Martin Beckett

Reputation: 96167

If you just want to display a simple image then make a Qlabel the central widget and call setPixmap() passing it your image path

"Another Question: Why does Qt have things like "QString" and "QChar"? Is there something wrong with the normal c++ libraries?"

Yes there is lots wrong with the normal libraries - at least for std::string.
When Qt was started there wasn't very good cross platform STL support and the standard libraries were very bad at Unicode and support for translations. QString doesthis very well - although I think a combination of modern STL and boost can probably do everything QString can do.

Almost all of the Qt types are automatically reference counted so you can pretty much ignore memory management for them AND pass them around freely. There are also some tricks Qt can do because the extra MOC compile pass means it has Java-like introspection that standard C++ doesn't.

But in general you are free to use standard C++ types (Qt: Qt classes vs. standard C++)

Upvotes: 5

jkerian
jkerian

Reputation: 17046

I think your problem is here:

{
    QGraphicsPixmapItem item(QPixmap("../images/tank2.bmp"));
    scene.addItem(&item); 
}

item goes out of scope before you'll actually use it.

I'm also pretty sure you meant that to use the QPixmap that you loaded earlier at the top-level scope.

Generally speaking, you want to limit your questions on SO to a single question... but to address your last question: QChar and QString allow the Qt libs to make several assumptions about strings. The most obvious of which is that QStrings have a standardized encoding.

Upvotes: 2

Related Questions