LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2005, 03:59 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
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?

Last edited by Ephracis; 04-08-2005 at 04:05 PM.
 
Old 04-08-2005, 10:53 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 04-08-2005, 11:01 PM   #3
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
try calling
repaint();
after you load that image to trigger the paintEvent
 
Old 06-27-2009, 06:10 AM   #4
max_the_boy
LQ Newbie
 
Registered: Jun 2009
Posts: 1

Rep: Reputation: 0
Now Qt can't write .GIF format. Just read.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Draw McGraw?? Sam Boyd Linux - Newbie 1 09-09-2005 06:16 PM
Can't draw curves in Photoshop 7 Cinematography Linux - Software 2 08-24-2005 09:27 AM
how to draw info from .pdf? pipehappy Linux - Software 1 01-22-2005 08:32 AM
Acorn Draw Clone proudclod Linux - Software 7 05-30-2004 12:34 AM
Draw Circle Gerardoj Programming 11 08-24-2003 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration