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.


  • Qt Champions 2017

    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.


  • Qt Champions 2017

    @CYLee

    Hi
    I wish i had a camera so i could test it fast but lets talk about the terms

    Subclassing:
    Make your own class based on other class

    class 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%

    alt text

    Changed project with custom widget.
    https://www.dropbox.com/s/r25lqvmj3lqimhg/camdraw.zip?dl=0

    To 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.


  • Qt Champions 2017

    @CYLee
    you the
    100,100 in drawText need to be changed.

    Can you show what you tried and "didnt work"?



  • @mrjj

    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;
    }
    

    0_1514211789580_圖片 1.png

    As you can see, the string disappeared.

    I set a breakpoint to trace drawText_x, the value didn't change.


  • Qt Champions 2017

    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 also

    the 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.
    }



  • @mrjj

    Problem solved!

    You really helped me a lot.

    Thank you for the detailed reply.


  • Qt Champions 2017

    @CYLee
    You are welcome. Glad it did work as i hoped and camera feed not drawn
    as overlay on widget :)


Log in to reply
 

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