Hi all,
I have a source of images that I wish to feed one after the other and show to the user.
For that end I created a QWidget application and tried to create a custom widget whose job is simply to paint the images it receives (code attached at the end of the post).
I use it by adding a widget in the Designer and promoting it to the proper class.
When running said widget the program crashes with the message "QWidget::repaint: Recursive repaint detected".
Any Ideas what might cause it?
Widget header:
#ifndef IMSTREAMWIDGET_H
#define IMSTREAMWIDGET_H
#include <QWidget>
#include <QImage>
#include <QPainter>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
class ImStreamWidget
: public QWidget{
Q_OBJECT
public:
virtual ~ImStreamWidget(){}
void updateImage(cv::Mat image);
private:
};
#endif // IMSTREAMWIDGET_H
#ifndef IMSTREAMWIDGET_H
#define IMSTREAMWIDGET_H
#include <QWidget>
#include <QImage>
#include <QPainter>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
class ImStreamWidget : public QWidget
{
Q_OBJECT
public:
explicit ImStreamWidget(QWidget *parent = 0) : QWidget(parent){}
virtual ~ImStreamWidget(){}
void updateImage(cv::Mat image);
void paintEvent(QPaintEvent* e);
private:
QImage *_image;
};
#endif // IMSTREAMWIDGET_H
To copy to clipboard, switch view to plain text mode
Widget source:
#include "imstreamwidget.h"
void ImStreamWidget::updateImage(cv::Mat image){
cv::cvtColor(image, image, CV_BGR2RGB);
// _image is created according to video dimensions
if (_image)
{
delete _image;
}
_image
= new QImage(image.
size().
width, image.
size().
height,
QImage::Format_RGB888);
// Copy cv::Mat to QImage
memcpy(_image->scanLine(0), (unsigned char*)image.data, _image->width() * _image->height() * image.channels());
// Resize the window to fit video dimensions
resize(image.size().width, image.size().height);
}
{
// When no image has been loaded, there's nothing to draw.
if (_image){
painter.
drawImage(QPoint(0,
0),
*_image
);
}
}
#include "imstreamwidget.h"
void ImStreamWidget::updateImage(cv::Mat image){
cv::cvtColor(image, image, CV_BGR2RGB);
// _image is created according to video dimensions
if (_image)
{
delete _image;
}
_image = new QImage(image.size().width, image.size().height, QImage::Format_RGB888);
// Copy cv::Mat to QImage
memcpy(_image->scanLine(0), (unsigned char*)image.data, _image->width() * _image->height() * image.channels());
// Resize the window to fit video dimensions
resize(image.size().width, image.size().height);
}
void ImStreamWidget::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
// When no image has been loaded, there's nothing to draw.
if (_image){
painter.drawImage(QPoint(0, 0), *_image);
}
QWidget::paintEvent(e);
}
To copy to clipboard, switch view to plain text mode
Bookmarks