PDA

View Full Version : qlabel can't use setPixmap() function



emailhy
23rd December 2009, 22:02
I build a widget has a qlabel, below is its constructor


#ifndef VIEWER_H
#define VIEWER_H

#include <iostream>

#include <QWidget>
#include <QPixmap>
#include <QLabel>
#include <QImage>
#include <QVBoxLayout>


using namespace std;

class Viewer : public QWidget
{
Q_OBJECT

public:
Viewer(QWidget *parent=0);
public slots:
void setIplImage();


private:

QVBoxLayout *mainLayout;
QLabel *movieLabel;


};

#endif

Viewer::Viewer(QWidget* parent)
: QWidget(parent)
{
movieLabel = new QLabel(tr("No movie loaded"));
movieLabel->setAlignment(Qt::AlignCenter);
movieLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
movieLabel->setBackgroundRole(QPalette::Dark);
movieLabel->setAutoFillBackground(true);
movieLabel->setScaledContents(true);



mainLayout = new QVBoxLayout;
mainLayout->addWidget(movieLabel);
setLayout(mainLayout);

}


void
Viewer::setIplImage()
{

QImage image("test.jpg");
//image.save("test2.jpg"); //it works

movieLabel->setPixmap(QPixmap::fromImage(image)); //compile success but runtime error with access violation.

}


and if i use it in constructor, the setPixmap() can run.
Thanks for any comments

Coises
25th December 2009, 00:30
Are you by any chance calling setIplImage(), or connecting to it using Qt::DirectConnection (http://doc.trolltech.com/4.6/qt.html#ConnectionType-enum), from a thread other than the GUI thread?