Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLabel, QPainter, QMouseEvent, and Mapping



  • Hello QT Family, I am working on an analysis system with a little bit of AI and ML implementation. I have a project that first opens a video and the user can play it. Upon a press of a button, it opens up another window and displays the frame that has been paused to allow user to draw lines using qmouse event. I then want to map the mouse events from the label on the popup window back to the video to help count items, cars, or people. I am currently having a challenge with drawing the lines. Here is the snippet of the code that draws the line.

    myQLabel.h
    #ifndef MYQLABEL_H
    #define MYQLABEL_H

    #include <QObject>
    #include <QWidget>
    #include <QLabel>
    #include <QMouseEvent>
    #include <QEvent>
    #include <QDebug>
    #include <QPainter>
    #include "debugger.h"
    class myQLabel : public QLabel
    {
    Q_OBJECT
    public:
    explicit myQLabel(QWidget * parent = nullptr);
    public:
    void mousePressEvent(QMouseEvent* ev);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    public:
    bool isMousePressed;
    bool drawStarted;
    bool isMouseMoving;
    public:
    int x, y;
    int lineStartX;
    int lineStartY;
    int lineStopX;
    int lineStopY;
    public:

    QPainter painter;
    QPixmap mPix;
    QLine mLine;
    QRect mRect;
    QPixmap mpPix;
    void drawLine();
    void drawRectangle();
    QPoint pointOne;
    QPoint pointTwo;
    QLine counterLine;
    

    };

    #endif // MYQLABEL_H

    myQLabel.cpp
    #include "myqlabel.h"

    myQLabel::myQLabel(QWidget* parent): QLabel(parent)
    {
    drawStarted = false;
    }
    void myQLabel::mousePressEvent(QMouseEvent *ev) {
    lineStartX = this->x = ev->x();
    lineStartY = this->y = ev->y();
    QPoint startPoint(lineStartX, lineStartY);
    counterLine.setP1(startPoint);

    }

    void myQLabel::mouseMoveEvent(QMouseEvent *event) {
    lineStopX = this->x = event->x();
    lineStopY = this->y = event->y();
    update();

    QPoint endPoint(lineStopX, lineStopY);
    counterLine.setP2(endPoint);
    QPixmap pix;
    if(this->pixmap() && !this->pixmap()->isNull()){
        pix = this->pixmap()->copy();
    }else{
        pix = QPixmap(this->size());
    }
    QPainter p(&pix);
    QPen pen(Qt::blue, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    p.setPen(pen);
    p.drawLine(counterLine);
    
    p.end();
    this->setPixmap(pix);
    QLabel::mousePressEvent(event);
    

    }

    void myQLabel::mouseReleaseEvent(QMouseEvent *event) {

    }
    I have promoted my label to myQLabel class. It works but seems to draw many lines and I believe the problem is in the paint event called in my mousemoveevent. Any help will be immensely appreciated.


  • Lifetime Qt Champion

    @vinnadipo09 You should not draw in mouseMoveEvent. You should only call update() there and do the painting in paintEvent.



  • @jsulm Thanks. I followed this https://forum.qt.io/topic/28902/solved-qmouseevent-qpainter-and-opencv/2 , after a long struggle and that is what I have implemented. I would appreciate any clear clarification from your end. I will implement it asap and get back to you on the outcome.


  • Lifetime Qt Champion

    @vinnadipo09 said in QLabel, QPainter, QMouseEvent, and Mapping:

    I would appreciate any clear clarification from your end

    Not sure what clarification you need. In mouseMoveEvent store all data needed to paint in member variables and call update().
    Take a look at https://doc.qt.io/qt-5/qpainter.html
    "The common use of QPainter is inside a widget's paint event"



  • Thanks. Let me work this out.


Log in to reply