LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I draw an image with Qt? (https://www.linuxquestions.org/questions/programming-9/how-do-i-draw-an-image-with-qt-311205/)

Ephracis 04-08-2005 03:59 PM

How do I draw an image with Qt?
 
I am having big problems trying to put an gif-image into an application written in Qt. I have removed all the other code and right now I am just trying to make an image, nothing else. The image-widget is in the class RedBar and that is the only class I have. So here are my files:

Code:

bar.h

#ifndef __BAR_H__
#define __BAR_H__

#include <qwidget.h>

class QImage;

class RedBar : public QWidget {
public:
        RedBar(const QString &filename, QWidget *parent, const char *name=0);

protected:
        void paintEvent(QPaintEvent*);

private:
        QImage *qimage;
};

#endif

Code:

bar.cc

#include <qimage.h>
#include <qpainter.h>

#include "bar.h"

RedBar::RedBar(const QString &filename, QWidget *parent, const char *name) : QWidget (parent, name) {
        qimage = new QImage;
        qimage->load(filename);
}

void RedBar::paintEvent(QPaintEvent*) {
        QPainter qpainter(this);
        qpainter.drawImage(0, 0, *qimage);
}


Code:

main.cc

#include <qapplication.h>

#include "bar.h"

int main(int argc, char *argv[]) {
        QApplication a(argc, argv);

        RedBar *bar1 = new RedBar(argv[1], 0);
        a.setMainWidget(bar1);
        bar1->show();
        return a.exec();
}

Then I compile it and run:
Code:

$ ./app redbar.gif
The code compiles without problems but when I run the program I just get a big grey app with nothing in it. The image redbar.gif is a 10x10 image created in gimp. What am I doing wrong?

paulsm4 04-08-2005 10:53 PM

Try saving your test file to a different format (e.g. .png) and see if it works.

If so, then your Qt libraries weren't compiled with .gif support (this is a non-default option).

'Hope that helps .. PSM

foo_bar_foo 04-08-2005 11:01 PM

try calling
repaint();
after you load that image to trigger the paintEvent

max_the_boy 06-27-2009 06:10 AM

Now Qt can't write .GIF format. Just read.


All times are GMT -5. The time now is 06:00 AM.