PDA

View Full Version : JPEG Image isn't displayed!?



GodOfWar
15th April 2007, 13:53
Hi there!
This my first time on this forum.
I'm creating an application for an exam and I need to display a JPEG image in the application.
Here is the code:



#include <QApplication>
#include <QImageReader>
#include <QWidget>

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
QImageReader *lettore;
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
lettore=new QImageReader("k8055.jpg");
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}


I'm using QT 4.2.3 Open Source with Visual Studio 2005.
When I run the application I see a blank dialog with the same image size. Where is the mistake? Can you help me?

PS. Sorry for bad english but I am Italian

marcel
15th April 2007, 14:09
#include <QApplication>
#include <QWidget>
#include <QPixmap>
#include <QPainter>
#include <QPaintEvent>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
QPixmap mPixmap;
protected:
virtual void paintEvent( QPaintEvent* );
};

MyWidget::MyWidget(QWidget *parent)

: QWidget(parent)
{
mPixmap = QPixmap( "k8055.jpg" );
setFixedSize( mPixmap.size() );
}


void MyWidget::paintEvent( QPaintEvent* e )
{
if( !mPixmap.isNull() )
{
QPainter painter( this );
p.drawPixmap( 0, 0, mPixmap );
}
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}

durbrak
15th April 2007, 14:10
Try using QLabel::setPixmap().

jpn
15th April 2007, 14:10
Maybe you don't have JPEG support turned on? What does

qDebug() << QImageReader::supportedImageFormats();
output? This requires reconfiguring and rebuilding Qt libs, see configure -help for more info.

marcel
15th April 2007, 14:10
Make sure the image is in the same folder as the executable. Otherwise, give the complete path when you create the QPixmap.

elcuco
15th April 2007, 19:52
[ot]

how are you using Qt4 OpenSource + Visual Studio?

marcel
15th April 2007, 20:01
The hardest part is to get Qt compiled.
To start a project, you just need to set the appropriate include paths and library paths in the project properties, just as Qt VS Integration does.

Regards

wysota
15th April 2007, 21:00
To start a project you just need to call "qmake -tp vc" :)

marcel
15th April 2007, 21:04
To start a project you just need to call "qmake -tp vc"

Or that... :)

GodOfWar
16th April 2007, 15:01
#include <QApplication>
#include <QWidget>
#include <QPixmap>
#include <QPainter>
#include <QPaintEvent>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
QPixmap mPixmap;
protected:
virtual void paintEvent( QPaintEvent* );
};

MyWidget::MyWidget(QWidget *parent)

: QWidget(parent)
{
mPixmap = QPixmap( "k8055.jpg" );
setFixedSize( mPixmap.size() );
}


void MyWidget::paintEvent( QPaintEvent* e )
{
if( !mPixmap.isNull() )
{
QPainter painter( this );
p.drawPixmap( 0, 0, mPixmap );
}
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}


I managed to make it work using this code thx:D ! (Actually I had to modify line 29).
@jpn
Where do I have to place that instruction in order to show something??

FOR ALL:
Can someone explain me why it doesn't work with the QImageReader?