【发布时间】:2011-05-07 13:13:02
【问题描述】:
我无法让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));
}
那么,我做错了什么?
为什么我在单击视图或使用鼠标滚轮时看不到消息或背景颜色变化?
【问题讨论】:
-
我不知道你在做什么,但是你为什么要在QGraphicsView里面新建QGraphicsView呢?考虑删除“view”-variable,而不是 view->setXxx 只是 setXxx。
-
是的,这正是问题所在。谢谢
标签: c++ qt events qt4 qgraphicsview