Solved Moving QLabel on QCameraViewfinder will blink
-
Dear Qt experts,
I used QT example "camera" to add a QLabel on the QCameraViewfinder.
And I used the Horizontal Slider and setGeometry to change the QLabel position.
The problem is, when the video active and I operate the slider.
The QLabel will blink when it moving.
Can someone tell me how to improve it?
Thanks. -
Hi
You can try subclass QCameraViewfinder and override its paintEvent.
Then in paintevent call base paint
and then draw your text.
That should prevent flickering.
Sadly i dont have any camera so i cant tell if it will work. -
@mrjj thank you for your reply.
I'm new in QT and C++.
So...could you be a bit more specific about implementation?
And I found the same problem when I import a png image on the QCameraViewfinder.
Should I use the same way to improve it?
Thank you. -
Hi
I wish i had a camera so i could test it fast but lets talk about the termsSubclassing:
Make your own class based on other classclass MyCameraViewfinder : public QCameraViewfinder { Q_OBJECT public: explicit MyCameraViewfinder(QWidget* parent = nullptr) : QCameraViewfinder(parent) { } protected: virtual void paintEvent(QPaintEvent* event) override { // override a base function with own // Default rendered -> call base class QCameraViewfinder::paintEvent(event); // draw some text QPainter painter(this); painter.drawText(100,100,"text"); }; };
You then use your instead of the default one.
You can new it and place it yourself or use promotion feature
http://doc.qt.io/qt-5/designer-using-custom-widgets.html
which makes it easy to use in Designer / the camera sample.
(it already use promotion that sample)Anyway, i test with camera sample and i think it will work but
since i cant see with camera on, i cant not know 100%Changed project with custom widget.
https://www.dropbox.com/s/r25lqvmj3lqimhg/camdraw.zip?dl=0To move it around, you just make the x,y of the drawtext variable/properties and
then change those. -
@mrjj thank you for your reply again.
How can I change drawtext variable/properties via slider or button?
I declared a variable and tried to change that value via Horizontal Slider.
But it doesn't work.
-
@CYLee
you the
100,100 in drawText need to be changed.Can you show what you tried and "didnt work"?
-
I'm sorry that I did not describe it well.
I declared a variable drawText_x and change that value via Horizontal Slider.
mycameraviewfinder.h class MyCameraViewfinder : public QCameraViewfinder { Q_OBJECT public: explicit MyCameraViewfinder(QWidget* parent = nullptr) : QCameraViewfinder(parent) { } int drawText_x; protected: virtual void paintEvent(QPaintEvent* event) override { // override a base function with own // Default rendered -> call base class QCameraViewfinder::paintEvent(event); // draw some text QPainter painter(this); painter.drawText(drawText_x, 100, "this is my text"); } }; camera.cpp void Camera::on_horizontalSlider_valueChanged(int value) { MyCameraViewfinder m; m.drawText_x = value; }
As you can see, the string disappeared.
I set a breakpoint to trace drawText_x, the value didn't change.
-
Hi
with 100,100 fixed value, is the string there ?
Just checking the idea works at all :)Regardless
void Camera::on_horizontalSlider_valueChanged(int value)
{
MyCameraViewfinder m; <<< local value, and other copy. not the one on screen
m.drawText_x = value;
} // dead here alsothe one in sample is changed to MyCameraViewfinder
so it should be
void Camera::on_horizontalSlider_valueChanged(int value)
{
ui->viewfinder-> drawText_x = value; // drawText_x is public.
} -
-
@CYLee
You are welcome. Glad it did work as i hoped and camera feed not drawn
as overlay on widget :)