PDA

View Full Version : Custom image streaming widget enters infinite recursion



yi gi
24th September 2015, 15:02
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:
explicit ImStreamWidget(QWidget *parent = 0) : QWidget(parent){}
virtual ~ImStreamWidget(){}
void updateImage(cv::Mat image);
void paintEvent(QPaintEvent* e);

private:
QImage *_image;
};

#endif // IMSTREAMWIDGET_H



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);
}

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);
}

d_stranz
24th September 2015, 15:50
QWidget:: paintEvent(e);

Don't do this.

yi gi
28th September 2015, 12:54
Hi d_stranz,
I've removed the line but the message "QWidget::repaint: Recursive repaint detected" persists. Anything else I might try?

d_stranz
29th September 2015, 00:00
Anything else I might try?

No idea. You haven't posted enough code to know how you are using the class. If you are resizing or otherwise changing the properties of the image widget from within an event handler that is called in response to a previous such event, then you'll get recursion.

yi gi
6th October 2015, 13:59
Currently I don't have any custom code in the project except for a few button clicking events that changes the active StackedWidget, everything else is strictly generated by the designer itself.

Added after 33 minutes:

If anyone has the same problem in the future:
The problem with the code above is that the variable _image is not initialized in the constructor, So if paintEvent is called before updateImage, the _image pointer will contain an invalid reference.