Custom image streaming widget enters infinite recursion
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:
Code:
#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
Widget source:
Code:
#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
);
}
}
Re: Custom image streaming widget enters infinite recursion
Quote:
QWidget:: paintEvent(e);
Don't do this.
Re: Custom image streaming widget enters infinite recursion
Hi d_stranz,
I've removed the line but the message "QWidget::repaint: Recursive repaint detected" persists. Anything else I might try?
Re: Custom image streaming widget enters infinite recursion
Quote:
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.
Re: Custom image streaming widget enters infinite recursion
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.