[SOLVED] Use QScollArea & QLabel to display image



  • Hello, everyone. I'm a freshman of Qt.
    Recently, I'm working on a frame grabber application. In order to display the image grabbed by the camera with vertical and horizontal scroll bar, I use QScollArea and QLabel. The code is as follow:
    https://gist.github.com/tianshilei1992/775234959c6bdbdd635b
    The grabber will call the callback function as long as it receives data. Perhaps due to the reason that the frame rate is too high, up to 400 fps, if I drag the scroll bar when the grabber is working, the application will crash.
    So how to fix this problem?





  • @Hamed Hi, thank you for your reply.
    I've read this. At present, the imageWindow, a QScrollArea class, could display the image correctly if I don't drag the scroll bar. When I drag the scroll bar, the application crashes. But if I click the arrow of scroll bar slowly, the application will not crash. So I guess it's probably because the fresh frequency is too high, when I drag the scroll bar, the application will re-draw the image, but the image has changed, so it crashed.


  • Qt Champions 2016

    @sheeley
    Hi
    I think crashes when scrolling as the
    imageLabel->setPixmap(QPixmap::fromImage(bmp));
    Might have deleted the old pix and about to set the new one
    when you then scroll.
    Since something else calls the callback, it is like an other thread and it
    seems like a multithreading issue.



  • @mrjj Hi.
    Yes, I guess so.
    Is there any solution for this issue? I have a software, made by the video capture card manufacture, which is also written by Qt, it could work well. So I guess there should be a solution. Perhaps asynchronous refresh?


  • Qt Champions 2016

    @sheeley
    Would it be an option just to show it scaled to screen so no scroll needed?

    Yeah, some sort of asynchronous refresh, or scolling.
    like after pix is set, its ok to scroll.
    Not sure how to program it though as you would need to capture the scroll event and
    check that setPixmap is not in progress and then scroll or or queue the scroll request and
    do the scrolling after setPixmap. But if you start to use a mutex or similar construct, you might end up skipping some frames.


  • Qt Champions 2016

    Hi
    I wonder if you do
    bool oldState = mageLabel->blockSignals(true);
    mageLabel->setPixmap(QPixmap::fromImage(bmp));
    mageLabel->blockSignals(oldState);
    Would help as to block any scrolling request while "in there".
    Not sure it uses signals for that though.
    Fast to test.



  • @mrjj Thank you for your help. But it could not work... :(


  • Qt Champions 2016

    @sheeley

    I guess the paint event is not blocked
    so it happens anyway.
    Since the scrollbars tells scroll area to scroll and it then the label repaints itself.

    If you build your own widget and draw the image in the paintevent, it would be
    easier to make sure its not painting when being updated, I guess.

    Do you really need to scroll the image at 400 fps ?
    Its like preview of the stream?



  • @mrjj Yes. This window is for preview of the stream. The stream is received from a high speed camera, its feature is high speed, up to 400 fps - -!
    I will think about building my own widget. Could you please show me some examples, if convenient.
    Thank you for your patience. :)


  • Qt Champions 2016

    @sheeley
    sketch code, fix to compile.
    Then if you lock the image when you set it and unlock
    when finished. and in paint check if ok to paint it.
    Something like that.

    class ImgPreview : public QWidget
    {
        Q_OBJECT
        
    public:
        ImgPreview(QWidget *parent = 0) : QWidget(parent) {};
    
    protected:    
        void paintEvent(QPaintEvent *event) {
       QPainter p ( this );
         if (ok_to_access_img)
          p.drawImage(image);
        }
    
    private:
        void setImagePixel() {}
        QImage image;
        
    };
    


  • @mrjj OK. Thank you. If any questions, I'll keep in touch with you later.



  • @mrjj Hi, I found some issues.
    If I overwrite the paint function, when I drag the scrollbars, the window will stay unchanged. This will lead to an un-continuous feeling, yes?


  • Qt Champions 2016

    @sheeley
    hi
    set you widget , minimum size to the image size to it will reflect the image. then
    scrolling works like with the QLabel and you can control when its safe to repaint.



  • @mrjj Hi.
    I'm very excited now. I fixed it with defining a new signal and slot to do the refreshing work. The code is as follow: https://gist.github.com/tianshilei1992/fd76520532127deb28de
    Done! The application won't crash, anymore! :)
    Thank you for your patience yesterday.


  • Qt Champions 2016

    @sheeley
    Good work!
    Thank you for update.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.