c++ - 重新实现鼠标事件时对 QGraphicsView 的奇怪影响

标签 c++ qt qgraphicsview

我使用这个源代码开始了一个小项目:

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QGraphicsLineItem>

class CustomRectItem : public QGraphicsRectItem
{
public:
    CustomRectItem (const QRectF& rect) : QGraphicsRectItem(rect) {
    setFlag(QGraphicsItem::ItemIsMovable);
    setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
    setAcceptHoverEvents(true);
}

void addLine(QGraphicsLineItem *line1, QGraphicsLineItem *line2) {
    if (this->data(0).toString() == "_p1") {
        this->leftLine = line1;
        this->topLine = line2;
    }
    if (this->data(0).toString() == "_p2") {
        this->topLine = line1;
        this->rightLine = line2;
    }
    if (this->data(0).toString() == "_p3") {
        this->rightLine = line1;
        this->bottomLine = line2;
    }
    if (this->data(0).toString() == "_p4") {
        this->bottomLine = line1;
        this->leftLine = line2;
    }
}

QPointF center(void) {
    return QPointF((rect().x() + rect().width() / 2),
                   (rect().y() + rect().height() / 2));
}

QVariant itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange && scene()) {
        // value is the new position.
        QPointF newPos = value.toPointF();

        moveLineToCenter(newPos, this->data(0).toString());
    }
    return QGraphicsItem::itemChange(change, value);
}

void moveLineToCenter(QPointF newPos, QString pointString) {
    // Converts the polygon upper left position
    // to the upper left "handle"-rect center position
    qreal xOffset = rect().x() + rect().width()/2;
    qreal yOffset = rect().y() + rect().height()/2;

    QPointF newCenterPos = QPointF(newPos.x() + xOffset, newPos.y() + yOffset);

    QPointF p1;
    QPointF p2;

 // upper-left point
    if (pointString == "_p1") {
        p1 = leftLine->line().p1();
        p2 = newCenterPos;
        leftLine->setLine(QLineF(p1, p2));
        p1 = newCenterPos;
        p2 = topLine->line().p2();
        topLine->setLine(QLineF(p1, p2));
  // upper-right point
    } else if (pointString == "_p2") {
        p1 = topLine->line().p1();
        p2 = newCenterPos;
        topLine->setLine(QLineF(p1, p2));
        p1 = newCenterPos;
        p2 = rightLine->line().p2();
        rightLine->setLine(QLineF(p1, p2));
  // lower-right point
    } else if (pointString == "_p3") {
        p1 = rightLine->line().p1();
        p2 = newCenterPos;
        rightLine->setLine(QLineF(p1, p2));
        p1 = newCenterPos;
        p2 = bottomLine->line().p2();
        bottomLine->setLine(QLineF(p1, p2));
  // lower-left point
    } else if (pointString == "_p4") {
        p1 = bottomLine->line().p1();
        p2 = newCenterPos;
        bottomLine->setLine(QLineF(p1, p2));
        p1 = newCenterPos;
        p2 = leftLine->line().p2();
        leftLine->setLine(QLineF(p1, p2));
    } else {
        return;
    }
}

/////////////////////////////////////////////////////
/* -- comment-in this block to observe problem --  //
/////////////////////////////////////////////////////

protected:
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
    Q_UNUSED(event);
    this->setCursor(Qt::SizeAllCursor);
}
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
    Q_UNUSED(event);
    this->unsetCursor();
}
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) {
    Q_UNUSED(event);
    this->setCursor(Qt::BlankCursor);
}
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
    Q_UNUSED(event);
    this->unsetCursor();
    this->data(0).toString();
    if (this->isUnderMouse()) {
           this->setCursor(Qt::SizeAllCursor);
    }
}

/////////////////////////////////////////////////////
//       -- end of problem-causing block --        //
///////////////////////////////////////////////////// */

private:
QGraphicsLineItem *topLine;
QGraphicsLineItem *rightLine;
QGraphicsLineItem *bottomLine;
QGraphicsLineItem *leftLine;
};

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    QGraphicsScene scene;

    CustomRectItem *custRect1 = new CustomRectItem(QRectF(30, 30, 10, 10));
    scene.addItem(custRect1);
    custRect1->setData(0, "_p1");

    CustomRectItem *custRect2 = new CustomRectItem(QRectF(70, 30, 10, 10));
    scene.addItem(custRect2);
    custRect2->setData(0, "_p2");

    CustomRectItem *custRect3 = new CustomRectItem(QRectF(70, 70, 10, 10));
    scene.addItem(custRect3);
    custRect3->setData(0, "_p3");

    CustomRectItem *custRect4 = new CustomRectItem(QRectF(30, 70, 10, 10));
    scene.addItem(custRect4);
    custRect4->setData(0, "_p4");

    QGraphicsLineItem *topLine = scene.addLine(QLineF(custRect1->center(), custRect2->center()));
    QGraphicsLineItem *rightLine = scene.addLine(QLineF(custRect2->center(), custRect3->center()));
    QGraphicsLineItem *bottomLine = scene.addLine(QLineF(custRect3->center(), custRect4->center()));
    QGraphicsLineItem *leftLine = scene.addLine(QLineF(custRect4->center(), custRect1->center()));

    custRect1->addLine(leftLine, topLine);
    custRect2->addLine(topLine, rightLine);

    custRect3->addLine(rightLine, bottomLine);
    custRect4->addLine(bottomLine, leftLine);

    QGraphicsView view(&scene);
    view.show();

    return a.exec();
}

编译上面的代码,你应该最终得到一个小窗口,其中只包含 QGraphivsView、一个多边形和四个连接到多边形顶点的 handle 。 现在使用鼠标移动 handle 。再次移动它。 一切都应该像您对任何图形编辑器的期望一样。 接下来,在重新实现一些鼠标事件的代码块中添加注释,基本上只是在将鼠标悬停在 handle 上并拖动 handle 时更改光标。 重新编译并重新开始移动一个句柄。再次移动同一个 handle ,然后……看看会发生什么!什么鬼?它跳回到它的初始位置。 有谁知道如何解释这种行为以及如何解决它?

谢谢!

最佳答案

确保在实现后调用基类事件以确保保持正常功能。

virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
    this->setCursor(Qt::SizeAllCursor);
    //
    QGraphicsRectItem::hoverEnterEvent( event );
}
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
    this->unsetCursor();
    //
    QGraphicsRectItem::hoverLeaveEvent( event );

}
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) {
    this->setCursor(Qt::BlankCursor);
    //
    QGraphicsRectItem::mousePressEvent( event );
}
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
    this->unsetCursor();
    this->data(0).toString();
    if (this->isUnderMouse()) {
           this->setCursor(Qt::SizeAllCursor);
    }
    //
    QGraphicsRectItem::mouseReleaseEvent( event );
}

关于c++ - 重新实现鼠标事件时对 QGraphicsView 的奇怪影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41129239/

相关文章:

c++ - 为 map 赋值的最有效方式

c++ - 使用 CSS (QSS) 选择器获取 QWidget*

qt - 关闭 Qt creator 5 : "Unexpected CDB error" 后的错误消息

c++ - 具有类似 QLineEdit 背景的 Qt Widget

c++ - 获取 QGraphicsView 的大小

c++ - 静态方法/字段的继承 C++

c++ - 在 Intellij 的 CLion 结构面板中显示类名

c++ - 自定义类型(自己的类)的 std::rank 的实现

c++ - qt在图形 View 中获取鼠标相对于图像的单击位置

python - Qt 在 QGraphicsView 中显示文本不失真,即使在 IgnoreAspectRatio View 中也是如此