QGraphicsView 中的事件
我在 QGraphicsView
中获取事件时遇到问题。我已经对 QGraphicsView 进行了子类化,并尝试重载 mousePressEvent 和wheelEvent。但 mousePressEvent
和 wheelEvent
都没有被调用。
这是我的代码(现在编辑了一些内容):
声明:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有收到事件,因为它们是由您正在创建的
scene
对象处理的,而不是您的类。从
rasImg
中删除QGraphicsScene *scene;
并为构造函数尝试类似的操作:如果您希望分两步完成,您可以这样做:
重点是场景显示的必须是
rasImg
的实际实例,而不是QGraphicsScene
的实例。如果您要子类化视图,请执行相同的操作。您显示的视图必须是您的类的实例,而不是普通的
QGraphicsView
。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 yourrasImg
and try something like this for the constructor:If you want that in two steps, you could do:
The point is that the scene that is displayed must be an actual instance of your
rasImg
, not an instance ofQGraphicsScene
.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 源自 QWidget。因此它像常规小部件一样接收鼠标事件。如果您的代码确实是
它无法接收事件,因为它应该是
QGraphicsSceneMouseEvent 用于QGraphicsScene 中接收鼠标输入的项目。
QGraphicsView is derived from QWidget. Therefore it receives mouse events like regular widgets. If your code really is
It can not receive events since it should be
QGraphicsSceneMouseEvent is for items in QGraphicsScene that receive mouse input.
如果您想处理特定 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 ofSimpleClass
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);