简体   繁体   English

QGraphicsView中的事件

[英]Events in QGraphicsView

I'm having trouble getting events in QGraphicsView working. 我在QGraphicsView运行事件时遇到了麻烦。 I've subclassed QGraphicsView and tried to overload the mousePressEvent and wheelEvent . 我已经将QGraphicsView子类化,并试图重载mousePressEventwheelEvent But neither mousePressEvent nor wheelEvent get called. 但是没有mousePressEventwheelEvent

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? 单击视图或使用鼠标滚轮时,为什么看不到消息或背景颜色变化?

You're not getting the events because they're being handled by the scene object you're creating, not your class. 你没有得到这些事件,因为它们是由你正在创建的scene对象处理的,而不是你的类。

Remove the QGraphicsScene *scene; 删除QGraphicsScene *scene; from your rasImg and try something like this for the constructor: 从你的rasImg并尝试这样的构造函数:

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 . 关键是显示的场景必须是rasImg的实际实例,而不是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 . 您正在显示的视图必须是您的类的实例,而不是简单的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);
}

QGraphicsView is derived from QWidget. QGraphicsView派生自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. QGraphicsSceneMouseEvent用于QGraphicsScene中接收鼠标输入的项目。

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, eg QGraphicsPixmapItem . 如果您想处理特定GUI元素的点击而不是处理整个场景的点击,您应该从QGraphicsItem这里参见SimpleClass示例)派生您自己的类,或者从现有元素之一派生,例如QGraphicsPixmapItem

In both cases in your derived class you can override void mousePressEvent(QGraphicsSceneMouseEvent *event); 在派生类的两种情况下,您都可以覆盖void mousePressEvent(QGraphicsSceneMouseEvent *event);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM