设置可拖动线段的限制

huangapple go评论134阅读模式
英文:

To set limit to draggable line segments

问题

mouseMoveEvent中的这个条件检查有一个问题。您可以尝试以下更改:

  1. if (dragging)
  2. {
  3. if (CurPoint1->y() > 50 && CurPoint1->y() < 150)
  4. {
  5. changeP1value(event->y());
  6. }
  7. else
  8. {
  9. // 如果超出了高度限制,您应该更新CurPoint1的y坐标,而不是忽略它
  10. if (CurPoint1->y() <= 50)
  11. {
  12. CurPoint1->setY(51); // 或者您可以设置为50,根据需要
  13. CurPoint2->setY(51); // 这将使线保持在50以上
  14. }
  15. else if (CurPoint1->y() >= 150)
  16. {
  17. CurPoint1->setY(149); // 或者您可以设置为150,根据需要
  18. CurPoint2->setY(149); // 这将使线保持在150以下
  19. }
  20. update();
  21. }
  22. }

这样,如果线的高度超出了50到150的限制,它将被设置回这些边界,从而允许您再次向下拖动。

英文:

I'm trying to implement segments that can be draggable by mouse between two points (shown below where green and red lines must be draggable between black dash lines).

sample image

设置可拖动线段的限制

I can drag the lines by mouse.

As a next step I want to set a limit to this draggable area, which I'm having issues to implement with. I really appreciate if someone can help me to do this. I now explain the situation in a bit detail:

.h is:

  1. public:
  2. MainWindow(QWidget *parent = nullptr);
  3. ~MainWindow();
  4. void paintEvent(QPaintEvent *e);
  5. public slots:
  6. void changeP1value(int);
  7. private:
  8. Ui::MainWindow *ui;
  9. bool dragging = false;
  10. QPoint p1 = QPoint(400,100);
  11. QPoint p2 = QPoint(450,100);
  12. QPoint p3 = QPoint(450,100);
  13. QPoint p4 = QPoint(500,100);
  14. QPoint *CurPoint1=nullptr;
  15. QPoint *CurPoint2=nullptr;
  16. protected:
  17. virtual void mousePressEvent(QMouseEvent *event) override;
  18. virtual void mouseReleaseEvent(QMouseEvent *event) override;
  19. virtual void mouseMoveEvent(QMouseEvent *event) override;
  20. };
  21. #endif // MAINWINDOW_H

and in .cpp file:

  1. // small helper to give us the distance
  2. int distance(QPoint x1, QPoint x2)
  3. {
  4. return abs(x2.y() - x1.y());
  5. }
  6. // the Paint event to draw lines
  7. void MainWindow::paintEvent(QPaintEvent *e)
  8. {
  9. QPainter painter1(this);
  10. QPen pointpen(Qt::black);
  11. pointpen.setWidth(5);
  12. QPen linepen1(Qt::red);
  13. linepen1.setWidth(2);
  14. QPen linepen2(Qt::green);
  15. linepen2.setWidth(2);
  16. painter1.setPen(linepen1);
  17. painter1.drawLine(p1,p2);
  18. painter1.setPen(linepen2);
  19. painter1.drawLine(p3,p4);
  20. }
  21. // when user clicks
  22. void MainWindow::mousePressEvent(QMouseEvent *event)
  23. {
  24. QPoint mp = event-&gt;pos(); // where is mouse
  25. // test if we hit the line. give user 20 pixels slack as its hard to hit one pixel
  26. if (distance ( mp, p1) &lt; 20 &amp;&amp; ( mp.x() &gt; p1.x() &amp;&amp; mp.x() &lt; p2.x() ) ) {
  27. dragging = true; // flag we are dragging
  28. CurPoint1 = &amp;p1;
  29. CurPoint2 = &amp;p2;
  30. }
  31. else if (distance ( mp, p3) &lt; 20 &amp;&amp; ( mp.x() &gt; p3.x() &amp;&amp; mp.x() &lt; p4.x() ) ) {
  32. dragging = true;
  33. CurPoint1 = &amp;p3;
  34. CurPoint2 = &amp;p4;
  35. }
  36. }
  37. void MainWindow::mouseReleaseEvent(QMouseEvent *event)
  38. {
  39. dragging = false; // if user release mouse we are not draggign anymore
  40. }
  41. // then when mouse move
  42. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  43. {
  44. // If we are dragging, call your normal slider changed function to update your points.
  45. if (dragging)
  46. {
  47. if(CurPoint1-&gt;y()&gt;50 &amp;&amp; CurPoint1-&gt;y()&lt;150)
  48. {
  49. changeP1value(event-&gt;y());
  50. }
  51. else
  52. update();
  53. }
  54. }
  55. void MainWindow::changeP1value(int value)
  56. {
  57. CurPoint1-&gt;setY(value);
  58. CurPoint2-&gt;setY(value);
  59. update();
  60. }

In the mouseMoveEvent, I've condition to check if I'm dragging a line and if the hight is between 50 and 150:

  1. if (dragging)
  2. {
  3. if(CurPoint1-&gt;y()&gt;50 &amp;&amp; CurPoint1-&gt;y()&lt;150) //----&gt; it get stucked and cannot be dragged down again
  4. {
  5. changeP1value(event-&gt;y());
  6. }
  7. else
  8. update();
  9. }

In this case I can drag , the line stops at the set limits but I cant drag down again. Can you spot an issue?

Thanks in advance

答案1

得分: 1

你需要检查新的y值,而不是当前值:

  1. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  2. {
  3. if (dragging)
  4. {
  5. if(event->y() > 50 && event->y() < 150)
  6. {
  7. changeP1value(event->y());
  8. }
  9. else
  10. update();
  11. }
  12. }
英文:

You need to check the new y value, not the current value:

  1. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  2. {
  3. if (dragging)
  4. {
  5. if(event-&gt;y() &gt; 50 &amp;&amp; event-&gt;y() &lt; 150)
  6. {
  7. changeP1value(event-&gt;y());
  8. }
  9. else
  10. update();
  11. }
  12. }

huangapple
  • 本文由 发表于 2020年1月6日 23:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定