【问题标题】:Events in QGraphicsViewQGraphicsView 中的事件
【发布时间】:2011-05-07 13:13:02
【问题描述】:

我无法让QGraphicsView 中的事件正常工作。我将QGraphicsView 子类化并尝试重载mousePressEventwheelEvent。但是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));
}

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

【问题讨论】:

  • 我不知道你在做什么,但是你为什么要在QGraphicsView里面新建QGraphicsView呢?考虑删除“view”-variable,而不是 view->setXxx 只是 setXxx。
  • 是的,这正是问题所在。谢谢

标签: c++ qt events qt4 qgraphicsview


【解决方案1】:

您没有收到事件,因为它们是由您正在创建的 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);
}

【讨论】:

  • 在我的实际代码中,我在另一个函数中初始化了 QGraphicsScene 和 QGraphicsView。为了简单起见,我把它放在构造函数中。现在看一下代码......我将如何继续?
  • 实际上我在代码中犯了一个错误...我已经将 qgraphicsview 子类化而不是 qgraphicsScene...我现在已经编辑了我的代码...你能看一下吗。
  • 谢谢,这是有道理的,真的很有帮助。我试试看
  • 谢谢,这太完美了...让我的代码运行...没有意识到我初始化了两个单独的 QGraphicsViews。
【解决方案2】:

QGraphicsView 派生自 QWidget。因此,它像常规小部件一样接收鼠标事件。如果你的代码真的是

void rasImg::mousePressEvent (QGraphicsSceneMouseEvent *event)

它不能接收事件,因为它应该是

void rasImg::mousePressEvent ( QMouseEvent *event )

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

【讨论】:

  • 那么您还有其他事情要做,因为我目前正在查看从使用 mousePressEvent(QMouseEvent*) 的 QGraphicsView 派生的类,并且它确实接收事件。尝试先做一些非常简单的事情来解决问题。
  • 您至少应该看到来自事件的调试输出。背景无法更改,因为您正在从未附加到接收这些事件的视图的场景中更改它。你那里的部分有点乱。
【解决方案3】:

如果您想处理对特定 GUI 元素的点击,而不是处理对整个场景的点击,您应该从 QGraphicsItem 派生您自己的类(参见 SimpleClass here 的示例)或从一个派生现有元素,例如QGraphicsPixmapItem

在派生类的这两种情况下,您都可以覆盖void mousePressEvent(QGraphicsSceneMouseEvent *event);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多