Mapping from a promoted class

  • Hello fam! Quick question, I have a promoted label that displays my video. I need to draw a line on the video to help count my cars. However, when I draw it and pass it to my frames, the location is distorted. I have tried maptoglobal but to no avail. Below is the code. Secondly, is there a way to delete a qline painted by qpainter after drawing the line? Thanks.

    #include <QObject>
    #include <QWidget>
    #include <QLabel>
    #include <QMouseEvent>
    #include <QEvent>
    #include <QDebug>
    #include <QPainter>
    #include <opencv2/core/core.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include "debugger.h"
    class CounterLinePainter : public QLabel
        explicit CounterLinePainter(QWidget * parent = nullptr);
        void mousePressEvent(QMouseEvent* ev);
        void mouseReleaseEvent(QMouseEvent* ev);
        void mouseMoveEvent(QMouseEvent *ev);
        void paintEvent(QPaintEvent *ev);
        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;
        QPoint lineStartPoint;
        QPoint lineStopPoint;
        bool drawMyLines;
        cv::Mat image;
    //    int* x, *y;
    //    int ii;
        QImage qImage;
        void sentLine(QLine countingLine);
    t#include "counterlinepainter.h"
    CounterLinePainter::CounterLinePainter(QWidget* parent): QLabel(parent)
        isMousePressed = false;
        drawStarted = false;
        drawMyLines = true;
    void CounterLinePainter::mousePressEvent(QMouseEvent *ev)
        lineStartX = this->x = ev->x();
        lineStartY = this->y = ev->y();
        QPoint lineStartPoint(lineStartX, lineStartY);
    void CounterLinePainter::mouseReleaseEvent(QMouseEvent *ev)
       emit sentLine(counterLine);
    void CounterLinePainter::mouseMoveEvent(QMouseEvent *ev)
        if(ev->type() == QEvent::MouseMove){
            lineStopX = this->x = ev->x();
            lineStopY = this->y = ev->y();
            QPoint endPoint(ev->pos());
    void CounterLinePainter::paintEvent(QPaintEvent *ev)
        QPainter painter(this);
        const QPixmap* pixmapPointer = pixmap();
             const QPixmap & pix = *pixmapPointer;
             qreal ratio = qMin(static_cast<qreal>(width()) / pix.width(), static_cast<qreal>(height()) / pix.height());
             QTransform transform;
             transform.scale(ratio, ratio);
             QRect sourceRect = transform.mapRect(ev->rect());
             painter.drawPixmap(ev->rect(), pix, sourceRect);
             QPen pen(Qt::darkGreen, 8, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
                 LOGx("from painter P1");
    void AnalysisWindow::receiveLineFromPainter(QLine newDrawnLine) {
        newLine = newDrawnLine;
        QPoint pointOne = newLine.p1();
        QPoint pointTwo = newLine.p2();
        LOGx("before map");
        videoPlayer->counterP1 = cv::Point(pointOne.x(), pointOne.y());
        videoPlayer->counterP2 = cv::Point(pointTwo.x(), pointTwo.y());
        LOGx("after map");
        videoPlayer->showCounterLines = true;


    [edit: Cleaned tags SGaist]

  • Lifetime Qt Champion

    @vinnadipo09 said in Mapping from a promoted class:

    is there a way to delete a qline painted by qpainter after drawing the line?

    No, you just don't draw it anymore when not needed.

    You should not use global coordinates when drawing on a widget.
    How/from where do you get the lines?

  • Thank you so much for your response
    But since I have promoted my qlabel to the painter class, it is using local coordinates of the label. I want to now show them on my video and to have the exact location, I need to have the global coordinates, right? Have you seen the Image I have attached?
    the lower green line is the one I am drawing with the qmouse event and the upper one is the one displayed on the video. Meaning that the latter is assuming global positon while the former is assuming local position.

  • Lifetime Qt Champion

    @vinnadipo09 said in Mapping from a promoted class:

    Have you seen the Image I have attached?

    No, I can't see any images in your posts.

    "But since I have promoted my qlabel to the painter class" - what do you mean by that? Also, do you use your label to show the frames from video? If you show the frames of the video in this label and draw lines you should use the local coordinates.
    And again: from where are the lines coming (their coordinates)?

  • Lifetime Qt Champion

    alt text

  • @jsulm I am getting my line from the promoted qlabel that also displays my video. I have promoted it to the class that I have attached in the code. The counterlinepainter class
    the image is here:
    For me to draw lines using mouse event and qlabel, I had to promote the label where I am drawing on to a painter class. Does that make sense?

  • Lifetime Qt Champion

    @vinnadipo09 said in Mapping from a promoted class:


    This line does not have any effect as it does not change the parameter, but instead it returns new (mapped) coordinates.

  • @jsulm I finally got it to work but now when I resize my window, the line disappears or the location is immensely distorted. Can I make it to change with the size of the label? Another thing, how do I stop drawing the line once it is set up? And finally, the line on the video is drawn via cv::Line an a frame, still battling with how to use cv::Line to make it local on qlable. your response helped me get the coordinates. I hope you have an idea of how I can implement this.

  • Lifetime Qt Champion

    @vinnadipo09 said in Mapping from a promoted class:

    Another thing, how do I stop drawing the line once it is set up?

    You just don't draw it in paintEvent. I don't understand what the problem is?
    As long as you draw the line in paintEvent it will be visible.

  • @jsulm Thanks a bunch! Done and done! Now my only battle is scaling my line with the label. Remember, the line is cvLine and not Qline in the video. Thank you once again for your support.

Log in to reply