在 QGraphicsScene 类中跟踪鼠标移动

发布于 2024-12-09 15:48:16 字数 369 浏览 1 评论 0原文

我对 QGraphicsScene 进行了子类化,并添加了方法 mouseMoveEvent 来处理鼠标移动事件。

我在 QGraphicsView 顶部创建了一个标尺,并让标尺跟踪鼠标移动。在 QGraphicsScene::mousemoveEvent 中,我显式调用标尺小部件的 mouseMoveEvent 。目的是让标尺知道当前鼠标位置。

现在看来,当我移动鼠标时,QGraphicsScene::mousemoveEvent 没有被调用。但是,如果我按下鼠标左键并在按住按钮的同时移动它,我就可以让它工作。

这不是我愿意看到的;我希望每当我将鼠标放在视图上并移动鼠标时都会调用此方法。

有什么解决方法吗?

I subclassed QGraphicsScene and added the method mouseMoveEvent to handle mouse move events.

I created a ruler on top of QGraphicsView and have the ruler tracking mouse movement. In QGraphicsScene::mousemoveEvent, I call mouseMoveEvent of the ruler widget explicitly. The purpose is to have the ruler aware of the current mouse position.

Now it seems that QGraphicsScene::mousemoveEvent is not called when I move the mouse. However, I can get it to work if I press the left mouse button and move it while holding the button.

This is not what I'd like to see; I'd like this method to be called whenever I place the mouse over the view and move the mouse.

Is there any workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榕城若虚 2024-12-16 15:48:16

QGraphicsView 文档中所述,视图负责将鼠标和键盘事件转换为场景事件并将其传播到场景:

您可以使用鼠标和键盘与场景中的项目进行交互。 QGraphicsView将鼠标和按键事件转换为场景事件(继承QGraphicsSceneEvent的事件),并将其转发到可视化场景。

由于鼠标移动事件仅在默认情况下按下按钮时发生,你需要 setMouseTracking(true) 在视图上首先生成移动事件,以便它可以将这些事件转发到场景。
或者,如果您不需要转换为场景坐标,您可以直接在视图中而不是在场景中重新实现 mouseMoveEvent。但在这种情况下,请确保调用基类 QGraphicsView:: mouseMoveEvent 在您的实现中,以便为场景中的项目正确生成悬停事件。

As stated in the QGraphicsView documentation, the view is responsible for translating mouse and keyboard events into scene events and propagating that to the scene:

You can interact with the items on the scene by using the mouse and keyboard. QGraphicsView translates the mouse and key events into scene events, (events that inherit QGraphicsSceneEvent,), and forward them to the visualized scene.

Since mouse move events only occur when a button is pressed by default, you need to setMouseTracking(true) on the view for the move events to generated in the first place, so that it can forward those to the scene.
Alternatively, if you don't need the translation to scene coordinates, you could reimplement the mouseMoveEvent in the view directly rather than in your scene. But in this case, make sure you call the base class QGraphicsView::mouseMoveEvent in your implementation, so that hover events are properly generated for the items in your scene.

软糖 2024-12-16 15:48:16

tgs.cpp

#include <QtGui>
#include "tgs.h"
#define _alto  300
#define _ancho 700
#include <QGraphicsSceneMouseEvent>

TGs::TGs(QObject *parent)
    :QGraphicsScene(parent)
{ // Constructor of Scene
    this->over = false;
}

void TGs::drawBackground(QPainter *painter, const QRectF &rect)
{

#define adjy 30
#define adjx 30

    int j = 0;
    int alto = 0;

    QPen pen;
    pen.setWidth(1);
    pen.setBrush(Qt::lightGray);
    painter->setPen(pen);   

    painter->drawText(-225, 10, this->str);
    alto = _alto;  // 50 + 2

    for(int i = 0; i < alto; ++i)
    {
        j = i * adjy - 17;

        painter->drawLine(QPoint(-210, j), QPoint(_ancho, j));
    }

    for(int i = 0; i < 300; ++i)
    {
        j = i * adjx - 210;

        painter->drawLine(QPoint(j, 0), QPoint(j, _ancho * 2));
    }
}

void TGs::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QString string = QString("%1, %2")
               .arg(mouseEvent->scenePos().x())
              .arg(mouseEvent->scenePos().y()); // Update the cursor position text
    this->str = string;
    this->update();
}

void TGs::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    this->update();
}

void TGs::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    this->update();
}

tgs.h

#ifndef TGS_H
#define TGS_H

#include <QObject>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>

QT_BEGIN_NAMESPACE

class QGraphicsSceneMouseEvent;
class QMenu;
class QPointF;
class QGraphicsLineItem;
class QFont;
class QGraphicsTextItem;
class QColor;

QT_END_NAMESPACE

class TGs : public QGraphicsScene
{
public:
    TGs(QObject *parent = 0);

public slots:
    void drawBackground(QPainter *painter, const QRectF &rect);
    void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

    bool over;
    QString str;
    QGraphicsTextItem cursor;
};

#endif // TGS_H

tgs.cpp:

#include <QtGui>
#include "tgs.h"
#define _alto  300
#define _ancho 700
#include <QGraphicsSceneMouseEvent>

TGs::TGs(QObject *parent)
    :QGraphicsScene(parent)
{ // Constructor of Scene
    this->over = false;
}

void TGs::drawBackground(QPainter *painter, const QRectF &rect)
{

#define adjy 30
#define adjx 30

    int j = 0;
    int alto = 0;

    QPen pen;
    pen.setWidth(1);
    pen.setBrush(Qt::lightGray);
    painter->setPen(pen);   

    painter->drawText(-225, 10, this->str);
    alto = _alto;  // 50 + 2

    for(int i = 0; i < alto; ++i)
    {
        j = i * adjy - 17;

        painter->drawLine(QPoint(-210, j), QPoint(_ancho, j));
    }

    for(int i = 0; i < 300; ++i)
    {
        j = i * adjx - 210;

        painter->drawLine(QPoint(j, 0), QPoint(j, _ancho * 2));
    }
}

void TGs::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QString string = QString("%1, %2")
               .arg(mouseEvent->scenePos().x())
              .arg(mouseEvent->scenePos().y()); // Update the cursor position text
    this->str = string;
    this->update();
}

void TGs::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    this->update();
}

void TGs::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    this->update();
}

tgs.h:

#ifndef TGS_H
#define TGS_H

#include <QObject>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>

QT_BEGIN_NAMESPACE

class QGraphicsSceneMouseEvent;
class QMenu;
class QPointF;
class QGraphicsLineItem;
class QFont;
class QGraphicsTextItem;
class QColor;

QT_END_NAMESPACE

class TGs : public QGraphicsScene
{
public:
    TGs(QObject *parent = 0);

public slots:
    void drawBackground(QPainter *painter, const QRectF &rect);
    void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

    bool over;
    QString str;
    QGraphicsTextItem cursor;
};

#endif // TGS_H
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文