Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel, QPainter, QMouseEvent, and Mapping
QtWS25 Last Chance

QLabel, QPainter, QMouseEvent, and Mapping

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 459 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vinnadipo09
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • V vinnadipo09

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • V Offline
        V Offline
        vinnadipo09
        wrote on last edited by
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • V vinnadipo09

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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"

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • V Offline
            V Offline
            vinnadipo09
            wrote on last edited by
            #5

            Thanks. Let me work this out.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved