Solved Draggable line segment
-
Hi
You need to keep the original y values (in its own variables in .h/class)
and then in mousepress store then values ( or value since they have same y both points)
so in
mouseMoveEvent you can check if its still within allowed drag range and if not then do not call
the ValueChanged.It will then stay with in your range.
-
I did this:
private: Ui::MainWindow *ui; bool dragging = false; QPoint p1 = QPoint(400,100); QPoint p2 = QPoint(450,100); QPoint p3 = QPoint(450,100); QPoint p4 = QPoint(500,100); QPoint *CurPoint1=nullptr; QPoint *CurPoint2=nullptr;
I'm a bit confused how to implement those conditons in
mousePressEvent
andmouseMoveEvent
If I do this in themouseMoveEvent
if (dragging) { if( CurPoint1->y()>50 && CurPoint1->y()<150) { changeP1value(event->y()); } }
It just stop update if I go beyond the limit. What might be the issue?
-
Hi
If it stops showing the line maybeif (dragging) { if( CurPoint1->y()>50 && CurPoint1->y()<150) { changeP1value(event->y()); } else update(); }
To redraw the point with the last points value.
-
Actually it just stops when I hit 50 or 150 , it is not updating after that.
I doubt if it's a problem with
mousePressEvent
, now I do this:void MainWindow::mousePressEvent(QMouseEvent *event) { QPoint mp = event->pos(); if (distance ( mp, p1) < 10 && ( mp.x() > p1.x() && mp.x() < p2.x() ) ) { dragging = true; CurPoint1 = &p1; CurPoint2 = &p2; } else if (distance ( mp, p3) < 10 && ( mp.x() > p3.x() && mp.x() < p4.x() ) ) { dragging = true; CurPoint1 = &p3; CurPoint2 = &p4; } } // then when mouse move void MainWindow::mouseMoveEvent(QMouseEvent *event) { // If we are dragging, call your normal slider changed function to update your points. if (dragging) { if( CurPoint1->y()>50 && CurPoint1->y()<150) { changeP1value(event->y()); } else update(); } } void MainWindow::changeP1value(int value) { CurPoint1->setY(value); CurPoint2->setY(value); update(); }
-
@viniltc said in Draggable line segment:
it is not updating after that.
You mean it stops drawing the line or in what way ?
-
I mean, if I drag beyond 50 or 150 (y values). The line just stops at those points and freezes, can't drag anymore.
-
@viniltc
Yes, was that not the goal ??
Or do u mean it get stucked and cannot be dragged down again ? -
@mrjj
Yeah, I can't drag down again. Once I hit 50 or 150. The line just freezes there. -
This is what I'm doing now, but the situation is same, the line gets stuck and can't drag down:
public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void paintEvent(QPaintEvent *e); public slots: void changeP1value(int); private: Ui::MainWindow *ui; bool dragging = false; QPoint p1 = QPoint(400,100); QPoint p2 = QPoint(450,100); QPoint p3 = QPoint(450,100); QPoint p4 = QPoint(500,100); QPoint *CurPoint1=nullptr; QPoint *CurPoint2=nullptr; protected: virtual void mousePressEvent(QMouseEvent *event) override; virtual void mouseReleaseEvent(QMouseEvent *event) override; virtual void mouseMoveEvent(QMouseEvent *event) override; }; #endif // MAINWINDOW_H
and in
.cpp
int distance(QPoint x1, QPoint x2) { return abs(x2.y() - x1.y()); } void MainWindow::paintEvent(QPaintEvent *e) { QPainter painter1(this); QPen pointpen(Qt::black); pointpen.setWidth(5); QPen linepen1(Qt::red); linepen1.setWidth(2); QPen linepen2(Qt::green); linepen2.setWidth(2); painter1.setPen(linepen1); painter1.drawLine(p1,p2); painter1.setPen(linepen2); painter1.drawLine(p3,p4); qDebug()<<"Values of p1.y and p2.y:"<<p1.y()<<"and"<<p2.y(); } void MainWindow::mousePressEvent(QMouseEvent *event) { QPoint mp = event->pos(); if (distance ( mp, p1) < 20 && ( mp.x() > p1.x() && mp.x() < p2.x() ) ) { dragging = true; CurPoint1 = &p1; CurPoint2 = &p2; } else if (distance ( mp, p3) < 20 && ( mp.x() > p3.x() && mp.x() < p4.x() ) ) { dragging = true; CurPoint1 = &p3; CurPoint2 = &p4; } } void MainWindow::mouseReleaseEvent(QMouseEvent *event) { dragging = false; } void MainWindow::mouseMoveEvent(QMouseEvent *event) { if(CurPoint1!=nullptr) qDebug()<<"Curr point is NOT null"; if (dragging) { if(CurPoint1->y()>50 && CurPoint1->y()<150) { changeP1value(event->y()); } else update(); } } void MainWindow::changeP1value(int value) { CurPoint1->setY(value); CurPoint2->setY(value); update(); }
Can you spot any spot any issues?
-
Hi
There is no errors as such but since we can continue drag mouse after the endpoint,
the mouse event coordinates drifts and hence even dragging is still true some of the other checks is not valid anymore. ( like you are not on the line anymore)I do not know any good solution to it currently.