Qt World Summit: Submit your Presentation

Display raw image data

  • Hi,

    I'm new to Qt.

    I'm using a QTabWidget. I want to display/paint raw image data in the tab area.

    Please guide me how can this be achieved. What factors/resources/widgets are to be considered to achieve this.

    Thanks for all suggesstion and support.

  • You can even use a simple label to display an image "(see this)":http://doc.trolltech.com/4.2/widgets-imageviewer.html
    or you can use QPixmap and QImage to get more control.

  • I see lot of examples which use QPixmap and QImage to display an image. But, I'm do not know how to use the same in my context.

    I have used Qt Designer to create my Mainwindow with a QTabWidget. Unable to understand where to add the peice of code so that I can associate the QTabWidget to show the QImage.

    In the file mainwindow.cpp, ui->setupUi(this) is used to display the screen. I see that I can access ui->MytabWidget.... Unable to understand how to proceed further.

    Please guide.

  • This is how you can show an image thru a label:

    QLabel* defaultCentralWidget = new QLabel( this );
    defaultCentralWidget->setPixmap( QPixmap( ":/logo/img/logo/logo.png") );
    defaultCentralWidget->setAlignment( Qt::AlignCenter );

    Now you can call ui->MyTabWidget->addTab( defaultCentralWidget, "My IMAGE");

  • My code is here as under:

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget parent) :
    ui(new Ui::MainWindow)
    defaultCentralWidget = new QLabel( this );
    defaultCentralWidget->setPixmap( QPixmap( ":Image.png") );
    defaultCentralWidget->setAlignment( Qt::AlignCenter );
    ui->tabWidget->addTab(defaultCentralWidget, "Image");



    delete ui;

    Image.png is in the same location as the this source file.
    Application fails. Please let me know where am I going wrong.

  • I'm not sure of, but I think setupUI should be the first call. Then you have to ensure that Image.png is in the application resources, since you are using a path with the semicolon.
    What error do you get exactly?

  • Application simple failed at run time. A window with message insisting to debug appears. I was not able figure out even while debugging. However for the time being I have removed using forms and have done the implemenation manually. So things work.

    Well, my actually intention was to display the raw image data. My code is here below:
    @ QByteArray imgData;
    QFile file("img.raw");
    if (!file.open(QIODevice::ReadOnly))
    qDebug("File Size: %d",file.bytesAvailable());
    imgData = file.readAll();
    QImage rawImg;
    bool falg = rawImg.loadFromData(imgData);
    qDebug("%d", falg);
    QPixmap pmap(555, 313);
    QPainter painter;
    QLabel *myLabel = new QLabel;

    The file img.raw, is just a matrix of size 555X315 holding all pixel values. I understand that loadfromdata requires standard image format and hence my code will obviously not work.

    Please guide me what additional changes or implementation are to be done to get this working. I have referred to this link: http://doc.qt.nokia.com/qq/qq17-imageio.html

    Can article mentioned in the above link solve my problem. Please Guide. I would like to know if my approach is right.
    Thank you.

  • What are messages displayed from qDebug()?
    Moreover why are you not using the simpler operatore << with qDebug()? and you are displaying a boolean as an integer. Anyway, I don't know why your application is crashing, post some more info.

  • What's the format of your "raw image"? Usually the term raw image refers to the unformatted data that you get out of high end digicams. I'm not sure if that's what you have.

    The docs of QImage::loadFromData() state clearly:

    bq. Loads an image from the first len bytes of the given binary data. Returns true if the image was successfully loaded; otherwise returns false.
    The loader attempts to read the image using the specified format, e.g., PNG or JPG. If format is not specified (which is the default), the loader probes the file for a header to guess the file format.

  • The raw image doesn't have a format as such (PNG, JPG). I just have pixel data. I understand the code, I have actually shown in the thread will certainly fail.

    I would like to understand how to display or render/paint/display this pixel data.

  • The supported image types of QImage are "listed here":http://doc.qt.nokia.com/4.7/qimage.html#reading-and-writing-image-files. If you want to read other formats, you will have to write your own plugin.

  • Right... Thank you so much. And I believe this is the reference for writing a plugin: http://doc.qt.nokia.com/qq/qq17-imageio.html

    Thank you once again.

  • Yes, that's a good starting point.

  • @TARGET = rawPlugin
    TEMPLATE = lib
    CONFIG = qt plugin
    VERSION = 1.0.0

    HEADERS += argbhandler.h
    SOURCES += argbplugin.cpp

    target.path += D:/Software/QT/src-4.7.1/plugins/imageformats/imageformats
    INSTALLS += target@

    I'm using the example source provided in the link to learn. My project file is as above. I have modified the example source according to my requirement. I understand that I have to do:

    bq. qmake file.pro
    nmake install

    I get an error saying :

    bq. LINK : fatal error LNK1104: cannot open file 'rawPlugin.lib'

    Please correct me, if I'm doing any thing wrong.
    Thank you very much.

Log in to reply