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;
{
Q_OBJECT
public:
public slots:
void setIplImage();
private:
};
#endif
{
movieLabel
= new QLabel(tr
("No movie loaded"));
movieLabel->setAlignment(Qt::AlignCenter);
movieLabel
->setBackgroundRole
(QPalette::Dark);
movieLabel->setAutoFillBackground(true);
movieLabel->setScaledContents(true);
mainLayout->addWidget(movieLabel);
setLayout(mainLayout);
}
void
Viewer::setIplImage()
{
//image.save("test2.jpg"); //it works
movieLabel
->setPixmap
(QPixmap::fromImage(image
));
//compile success but runtime error with access violation.
}
#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.
}
To copy to clipboard, switch view to plain text mode
and if i use it in constructor, the setPixmap() can run.
Thanks for any comments
Bookmarks