设置可拖动线段的限制

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

To set limit to draggable line segments

问题

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

if (dragging)
{
    if (CurPoint1->y() > 50 && CurPoint1->y() < 150)
    {
        changeP1value(event->y());
    }
    else
    {
        // 如果超出了高度限制,您应该更新CurPoint1的y坐标,而不是忽略它
        if (CurPoint1->y() <= 50)
        {
            CurPoint1->setY(51); // 或者您可以设置为50,根据需要
            CurPoint2->setY(51); // 这将使线保持在50以上
        }
        else if (CurPoint1->y() >= 150)
        {
            CurPoint1->setY(149); // 或者您可以设置为150,根据需要
            CurPoint2->setY(149); // 这将使线保持在150以下
        }
        update();
    }
}

这样,如果线的高度超出了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:

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 file:

// small helper to give us the distance
int distance(QPoint x1, QPoint x2)
{
    return abs(x2.y() - x1.y());
}

// the Paint event to draw lines
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);

}

// when user clicks
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    QPoint mp = event-&gt;pos(); // where is mouse
// test if we hit the line. give user 20 pixels slack as its hard to hit one pixel
    if (distance ( mp, p1) &lt; 20 &amp;&amp; ( mp.x() &gt; p1.x() &amp;&amp; mp.x() &lt; p2.x() ) ) {
        dragging = true; // flag we are dragging
        
        
        CurPoint1 = &amp;p1;
        CurPoint2 = &amp;p2;
    }
    else if (distance ( mp, p3) &lt; 20 &amp;&amp; ( mp.x() &gt; p3.x() &amp;&amp; mp.x() &lt; p4.x() ) ) {
        dragging = true;
        CurPoint1 = &amp;p3;
        CurPoint2 = &amp;p4;
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    dragging = false; // if user release mouse we are not draggign anymore
}

// 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-&gt;y()&gt;50 &amp;&amp; CurPoint1-&gt;y()&lt;150) 
        {
        changeP1value(event-&gt;y());
        }
      else
      update();
    }

}

void MainWindow::changeP1value(int value)
{

    CurPoint1-&gt;setY(value);
    CurPoint2-&gt;setY(value);
    update();

}

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

 if (dragging)
    {
      if(CurPoint1-&gt;y()&gt;50 &amp;&amp; CurPoint1-&gt;y()&lt;150) //----&gt; it get stucked and cannot be dragged down again 
        {
        changeP1value(event-&gt;y());
        }
      else
      update();
    }

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值,而不是当前值:

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
  if (dragging)
  {
    if(event->y() > 50 && event->y() < 150) 
    {
      changeP1value(event->y());
    }
    else
      update();
  }
}
英文:

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

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
  if (dragging)
  {
    if(event-&gt;y() &gt; 50 &amp;&amp; event-&gt;y() &lt; 150) 
    {
      changeP1value(event-&gt;y());
    }
    else
      update();
  }
}

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:

确定