[SOLVED] Use QScollArea & QLabel to display image
-
Hi
Did you look at page below?
http://doc.qt.io/qt-5/qtwidgets-widgets-imageviewer-example.html -
@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. -
@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? -
@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. -
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... :(
-
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. :) -
@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? -
@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. -
@sheeley
Good work!
Thank you for update. -
Hi,
I followed your link but it is not available. I am interested in how you solved the problem. I am also trying to display video stream on QLabel with high framerate and large size of image. However, I suffer from screen tearing effect.
Thanks. -
@phnhung98 said in [SOLVED] Use QScollArea & QLabel to display image:
Hi,
I followed your link but it is not available. I am interested in how you solved the problem. I am also trying to display video stream on QLabel with high framerate and large size of image. However, I suffer from screen tearing effect.
Thanks.Hi and welcome to devnet,
Please define high speed and large size.