2

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?

Community
  • 1
  • 1
jason dancks
  • 1,152
  • 3
  • 9
  • 29
  • 1
    Regarding the "Headers", "Sources", etc... yes... those are just for visual clarity. For making build systems work across various systems, it's simpler to keep everything together in the file system. – jkerian Aug 16 '12 at 03:29
  • I think you have the wrong link there. – jkerian Aug 16 '12 at 03:29
  • 1
    Qt has been in active development well before C++98's release and before any production quality implementation of std::string was regularly obtainable. This made QString superior to any external implementation for a good 10 years. Likewise, at this point, redesigning Qt to use C++ libraries requires they test on a wide range of available C++ libraries that may not agree upon behavior and ABI. A great example of this is libstdc++ and libc++; they are NOT compatible... so Qt would have to support both... test on both... implement workarounds for both... on all architectures... etc. – dans3itz Aug 16 '12 at 03:48
  • Is this the correct link? http://stackoverflow.com/questions/4474086/display-qimage-on-qt-gui The one you included points to a totally unrelated question. – jogojapan Aug 16 '12 at 04:05
  • Thats an interesting question but thats not the link. I don't know what the question was exactly. – jason dancks Aug 17 '12 at 15:10
  • Btw, unless you are planning to do also other things with QGraphicsView, you could also display the image using QLabel, much simpler. And one thing to check is, when using relative paths, that the working directory is what you expect. – hyde Apr 04 '15 at 18:47

3 Answers3

5

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++)

Community
  • 1
  • 1
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
2

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.

jkerian
  • 16,497
  • 3
  • 46
  • 59
2

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
Rustam Safin
  • 812
  • 7
  • 15
  • This is basically the same code except you declare the QGraphicsPixmapItem as a pointer. As such it didn't work. I'm gonna look into this qrc file thing that might be where I messed up. OK I just made: nothing happened. dang. – jason dancks Aug 16 '12 at 16:17
  • Graphics scene doesn't show image if you're not creating new pointer. – Rustam Safin Aug 17 '12 at 04:27
  • OK. I tried what you had up there exactly. I really messed something up. For starters: Is this being made in QtCreator? Is this Qt quick GUI or something else?? and I just need main.cpp .pro and .qrc? and .ui. Anything I'm forgetting? – jason dancks Aug 17 '12 at 15:41
  • I just started QtCreator, created new project "testimage", copy-pasted code above. Then I opened GIMP, created simple image, saved to images folder as .bmp file. After that I returned to QtCreator and clicked "Run". It works. – Rustam Safin Aug 18 '12 at 18:04
  • ok I did just that, I ran that code with one modification: QGraphicsPixmapItem *item = new QGraphicsPixmapItem(qp); and output: Starting /Users/maureenwatts/Dropbox/qttester/stackoverflowtest2-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Debug/stackoverflowtest2.app/Contents/MacOS/stackoverflowtest2... Yes its null /Users/maureenwatts/Dropbox/qttester/stackoverflowtest2-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Debug/stackoverflowtest2.app/Contents/MacOS/stackoverflowtest2 exited with code 0 – jason dancks Aug 19 '12 at 19:28
  • Ok , It tried it again, this time I put a copy of the image into the project file and it finally worked. – jason dancks Aug 20 '12 at 18:08