PDA

View Full Version : QImage not loading successively.



node_ex
21st July 2008, 11:05
Hi,

I am loading a image from file using QImage class. After processing some steps, my application will load the image from other file to the same object. Application is exiting by segmentation fault. My Qt version is 4.4. Please provide me the solution if QImage does't handle two successive image loading.

Regards,
node_ex.

wysota
21st July 2008, 11:10
Please provide some code that you suspect that causes the segfault.

node_ex
21st July 2008, 14:48
Hi Wysota,

Actually i figured out the issue but unable to resolve it. I am launching a thread before exec. This hinders Qimage from loading an image from file.

Regards,
node_ex.

Code :

-----------------displayimage.h--------------------

#include <QWidget>
#include <qevent.h>
//#include <QLabel>
#include <QPainter>
#include <QImage>
#include <QPaintEvent>


class displayimage:public QWidget
{
private:
QImage _image;

protected:
virtual void paintEvent(QPaintEvent*);
public:
QString _fileName;
displayimage(QString);
~displayimage();

};

------------------displayimage.cpp----------------

#include <fstream.h>
#include <iostream.h>
#include <QMessageBox>
#include <QLabel>
#include "displayimage.h"

displayimage::displayimage(QString fileName)
{

_fileName=fileName;
setWindowTitle(tr("Node Explorer"));


}

displayimage::~displayimage()
{

}

void displayimage::paintEvent(QPaintEvent* paintEvent)
{
QPainter paint(this);

cout<<"B4 loading"<<endl;
_image.load(_fileName);

paint.drawImage(paint.window(),_image);

}

---------------main.cpp-------------------------

#include <QApplication>
#include <iostream.h>
#include "displayimage.h"
#include "testthread.h"
//using namespace std;

displayimage* obj;


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

QString fileName("/scratchbox/Venkanna.jpg");

obj = new displayimage(fileName);

cout<<"B4 show"<<endl;
obj->show();
Test test;
test.start();
int ret = app.exec();
return ret;
}

------------------testthread.h-------------------------

#include<qthread.h>

class Test: public QThread
{
public :
virtual void run();
};

void Test::run()
{

for(int count =0; count<20; count++)
{
sleep(1);
qDebug(" Thread started");
}
}

wysota
26th July 2008, 14:20
What if you load the file in the constructor and not in the paint event?