Important: Please read the 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.

    #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
    explicit myQLabel(QWidget * parent = nullptr);
    void mousePressEvent(QMouseEvent* ev);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    bool isMousePressed;
    bool drawStarted;
    bool isMouseMoving;
    int x, y;
    int lineStartX;
    int lineStartY;
    int lineStopX;
    int lineStopY;

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


    #endif // MYQLABEL_H

    #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);


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

    QPoint endPoint(lineStopX, lineStopY);
    QPixmap pix;
    if(this->pixmap() && !this->pixmap()->isNull()){
        pix = this->pixmap()->copy();
        pix = QPixmap(this->size());
    QPainter p(&pix);
    QPen pen(Qt::blue, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);


    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 , 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
    "The common use of QPainter is inside a widget's paint event"

  • Thanks. Let me work this out.

Log in to reply