Results 1 to 5 of 5

Thread: Custom image streaming widget enters infinite recursion

  1. #1
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. #ifndef IMSTREAMWIDGET_H
    2. #define IMSTREAMWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QImage>
    6. #include <QPainter>
    7. #include <opencv2\core\core.hpp>
    8. #include <opencv2\highgui\highgui.hpp>
    9. #include <opencv2/imgproc/imgproc.hpp>
    10.  
    11. class ImStreamWidget : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit ImStreamWidget(QWidget *parent = 0) : QWidget(parent){}
    17. virtual ~ImStreamWidget(){}
    18. void updateImage(cv::Mat image);
    19. void paintEvent(QPaintEvent* e);
    20.  
    21. private:
    22. QImage *_image;
    23. };
    24.  
    25. #endif // IMSTREAMWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Widget source:
    Qt Code:
    1. #include "imstreamwidget.h"
    2.  
    3. void ImStreamWidget::updateImage(cv::Mat image){
    4.  
    5. cv::cvtColor(image, image, CV_BGR2RGB);
    6.  
    7. // _image is created according to video dimensions
    8. if (_image)
    9. {
    10. delete _image;
    11. }
    12. _image = new QImage(image.size().width, image.size().height, QImage::Format_RGB888);
    13.  
    14. // Copy cv::Mat to QImage
    15. memcpy(_image->scanLine(0), (unsigned char*)image.data, _image->width() * _image->height() * image.channels());
    16.  
    17. // Resize the window to fit video dimensions
    18. resize(image.size().width, image.size().height);
    19. }
    20.  
    21. void ImStreamWidget::paintEvent(QPaintEvent* e)
    22. {
    23. QPainter painter(this);
    24.  
    25. // When no image has been loaded, there's nothing to draw.
    26. if (_image){
    27. painter.drawImage(QPoint(0, 0), *_image);
    28. }
    29. QWidget::paintEvent(e);
    30. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom image streaming widget enters infinite recursion

    QWidget:: paintEvent(e);
    Don't do this.

  3. #3
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default 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?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom image streaming widget enters infinite recursion

    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.

  5. #5
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    Last edited by yi gi; 6th October 2015 at 13:59.

Similar Threads

  1. image streaming with QT
    By saman_artorious in forum Qt Programming
    Replies: 1
    Last Post: 17th August 2013, 11:32
  2. trackball widget and infinite movement
    By alecail in forum General Programming
    Replies: 0
    Last Post: 26th March 2012, 00:58
  3. Problems with Image Streaming
    By gorsanmo in forum Newbie
    Replies: 1
    Last Post: 26th April 2011, 17:31
  4. Replies: 10
    Last Post: 10th November 2010, 03:12
  5. Replies: 5
    Last Post: 20th November 2007, 12:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.