QGraphicsView 中的事件

发布于 2024-11-05 11:38:07 字数 1276 浏览 2 评论 0原文

我在 QGraphicsView 中获取事件时遇到问题。我已经对 QGraphicsView 进行了子类化,并尝试重载 mousePressEvent 和wheelEvent。但 mousePressEventwheelEvent 都没有被调用。

这是我的代码(现在编辑了一些内容):

声明:

#include <QGraphicsView>
#include <QGraphicsScene>
class rasImg: public QGraphicsView
{
public:
rasImg(QString file);
~rasImg(void);
    initialize();
QGraphicsView *view;
QGraphicsScene *scene;
private:
virtual void mousePressEvent (QGraphicsSceneMouseEvent  *event);
virtual void wheelEvent ( QGraphicsSceneMouseEvent  * event );
}

定义:

#include "rasImg.h"
void rasImg::initialize()
{
view = new QGraphicsView();
scene = new QGraphicsScene(QRect(0, 0, MaxRow, MaxCol));
scene->addText("HELLO");
scene->setBackgroundBrush(QColor(100,100,100));
view->setDragMode(QGraphicsView::ScrollHandDrag);
view->setScene(scene);
}
void rasImg::mousePressEvent (QGraphicsSceneMouseEvent  *event)
{
    qDebug()<<"Mouse released";
    scene->setBackgroundBrush(QColor(100,0,0));
}
void rasImg::wheelEvent ( QGraphicsSceneMouseEvent  * event )
{
qDebug()<<"Mouse released";
    scene->setBackgroundBrush(QColor(100,0,0));
}

那么,我做错了什么?。
为什么当我单击视图或使用鼠标滚轮时看不到消息或背景颜色更改?

I'm having trouble getting events in QGraphicsView working. I've subclassed QGraphicsView and tried to overload the mousePressEvent and wheelEvent. But neither mousePressEvent nor wheelEvent get called.

Here's my code (Edited a few things now):

Declaration:

#include <QGraphicsView>
#include <QGraphicsScene>
class rasImg: public QGraphicsView
{
public:
rasImg(QString file);
~rasImg(void);
    initialize();
QGraphicsView *view;
QGraphicsScene *scene;
private:
virtual void mousePressEvent (QGraphicsSceneMouseEvent  *event);
virtual void wheelEvent ( QGraphicsSceneMouseEvent  * event );
}

Definition:

#include "rasImg.h"
void rasImg::initialize()
{
view = new QGraphicsView();
scene = new QGraphicsScene(QRect(0, 0, MaxRow, MaxCol));
scene->addText("HELLO");
scene->setBackgroundBrush(QColor(100,100,100));
view->setDragMode(QGraphicsView::ScrollHandDrag);
view->setScene(scene);
}
void rasImg::mousePressEvent (QGraphicsSceneMouseEvent  *event)
{
    qDebug()<<"Mouse released";
    scene->setBackgroundBrush(QColor(100,0,0));
}
void rasImg::wheelEvent ( QGraphicsSceneMouseEvent  * event )
{
qDebug()<<"Mouse released";
    scene->setBackgroundBrush(QColor(100,0,0));
}

So, what am I doing wrong?.
Why don't I see a message or background color change when I click the view or use the mouse wheel?

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

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

发布评论

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

评论(3

熟人话多 2024-11-12 11:38:07

您没有收到事件,因为它们是由您正在创建的 scene 对象处理的,而不是您的类。

rasImg 中删除 QGraphicsScene *scene; 并为构造函数尝试类似的操作:

rasImg::rasImg(QString file)
  : QGraphicsScene(QRect(0, 0, MaxRow, MaxCol))
{
 addText("HELLO");
 setBackgroundBrush(QColor(100,100,100));
 setDragMode(QGraphicsView::ScrollHandDrag);

 view = new QGraphicsView();
 view->setScene(this);
}

如果您希望分两步完成,您可以这样做:

rasImg::rasImg(QString file)
  : QGraphicsScene()
{
  // any constructor work you need to do
}


rasImg::initialize()
{
 addText("HELLO");
 setSceneRect(QRect(0, 0, MaxRow, MaxCol));
 setBackgroundBrush(QColor(100,100,100));
 setDragMode(QGraphicsView::ScrollHandDrag);

 view = new QGraphicsView();
 view->setScene(this);
}

重点是场景显示的必须是 rasImg 的实际实例,而不是 QGraphicsScene 的实例。

如果您要子类化视图,请执行相同的操作。您显示的视图必须是您的类的实例,而不是普通的QGraphicsView

rasImg::rasImg(QString file)
    : QGraphicsView()
{
   // constructor work
}

void rasImg::initialize()
{
  scene = new QGraphicsScene(QRect(0, 0, MaxRow, MaxCol));
  scene->addText("HELLO");
  scene->setBackgroundBrush(QColor(100,100,100));

  setDragMode(QGraphicsView::ScrollHandDrag);
  setScene(scene);
}

You're not getting the events because they're being handled by the scene object you're creating, not your class.

Remove the QGraphicsScene *scene; from your rasImg and try something like this for the constructor:

rasImg::rasImg(QString file)
  : QGraphicsScene(QRect(0, 0, MaxRow, MaxCol))
{
 addText("HELLO");
 setBackgroundBrush(QColor(100,100,100));
 setDragMode(QGraphicsView::ScrollHandDrag);

 view = new QGraphicsView();
 view->setScene(this);
}

If you want that in two steps, you could do:

rasImg::rasImg(QString file)
  : QGraphicsScene()
{
  // any constructor work you need to do
}


rasImg::initialize()
{
 addText("HELLO");
 setSceneRect(QRect(0, 0, MaxRow, MaxCol));
 setBackgroundBrush(QColor(100,100,100));
 setDragMode(QGraphicsView::ScrollHandDrag);

 view = new QGraphicsView();
 view->setScene(this);
}

The point is that the scene that is displayed must be an actual instance of your rasImg, not an instance of QGraphicsScene.

If it's the view you're subclassing, do the same thing. The view you're displaying must be an instance of your class, not a plain QGraphicsView.

rasImg::rasImg(QString file)
    : QGraphicsView()
{
   // constructor work
}

void rasImg::initialize()
{
  scene = new QGraphicsScene(QRect(0, 0, MaxRow, MaxCol));
  scene->addText("HELLO");
  scene->setBackgroundBrush(QColor(100,100,100));

  setDragMode(QGraphicsView::ScrollHandDrag);
  setScene(scene);
}
哭了丶谁疼 2024-11-12 11:38:07

QGraphicsView 源自 QWidget。因此它像常规小部件一样接收鼠标事件。如果您的代码确实是

void rasImg::mousePressEvent (QGraphicsSceneMouseEvent *event)

它无法接收事件,因为它应该是

void rasImg::mousePressEvent ( QMouseEvent *event )

QGraphicsSceneMouseEvent 用于QGraphicsScene 中接收鼠标输入的项目。

QGraphicsView is derived from QWidget. Therefore it receives mouse events like regular widgets. If your code really is

void rasImg::mousePressEvent (QGraphicsSceneMouseEvent *event)

It can not receive events since it should be

void rasImg::mousePressEvent ( QMouseEvent *event )

QGraphicsSceneMouseEvent is for items in QGraphicsScene that receive mouse input.

旧城空念 2024-11-12 11:38:07

如果您想处理特定 GUI 元素上的点击而不是处理整个场景上的点击,您应该从 QGraphicsItem 派生您自己的类(请参阅 SimpleClass 的示例< a href="http://doc.qt.io/archives/qt-4.7/qgraphicsitem.html" rel="nofollow">此处)或从现有元素之一派生,例如QGraphicsPixmapItem

在这两种情况下,您都可以在派生类中重写 void mousePressEvent(QGraphicsSceneMouseEvent *event);

If you would like to handle clicks on a specific GUI element rather than handle click on the whole scene, you should derive your own class either from QGraphicsItem (see example of SimpleClass here) or derive from one of existing elements, e.g. QGraphicsPixmapItem.

In both cases in your derived class you can override void mousePressEvent(QGraphicsSceneMouseEvent *event);

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