QGraphicsview mouserelease事件未注册

时间:2011-10-11 11:17:21

标签: qt mouseevent qgraphicsview

我试图通过事件过滤器获取mousepress和mouserelease事件的场景坐标。 它适用于mousepress,但是鼠标释放似乎没有注册。

我在这里缺少什么?

请注意我使用的是Qt Creator 2.2.0(基于Qt 4.7.4)

class mouseEater : public QObject{
// Q_OBJECT
protected:
 bool eventFilter(QObject *obj, QEvent *event);
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

mouseEater *eater = new mouseEater();

QGraphicsScene *scene;
scene = new QGraphicsScene();
ui->view->installEventFilter(eater);
ui->view->setScene(scene);
}

bool mouseEater::eventFilter(QObject *obj, QEvent *event){

QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);

QPoint point = mEvent->pos();

cout<<"type:"<<event->type()<<endl;

switch(event->type()){
case QEvent::MouseButtonPress:

    cout<<"mousepress at "<<point.x()<<","<<point.y()<<endl;
    return true;
    break;
case QEvent::MouseButtonRelease:
    cout<<"mouserelease at "<<point.x()<<","<<point.y()<<endl;
    return true;
    break;
default:
    //standard event processing
    return QObject::eventFilter(obj, event);
    break;
}
return 0;
}

2 个答案:

答案 0 :(得分:1)

快速回答是QGraphicsView似乎不允许其他QObjects吃掉它的mouseReleaseEvents。我不确定为什么,但我认为这与如何在QGraphicsView中重新实现mouseReleaseEvent()有关。一点点黑客行为提供了一个解决方法:

HEADER:

class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

protected:
  virtual bool eventFilter(QObject *, QEvent *);

private:
  Ui::MainWindow *ui;
};

class cDerived : public QGraphicsView
{
  Q_OBJECT

public:
  cDerived(QWidget *parent) : QGraphicsView(parent) {}

protected:
  virtual void mousePressEvent(QMouseEvent *event);
  virtual void mouseReleaseEvent(QMouseEvent *event);
};

来源:

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  QGraphicsView *View = new cDerived(ui->centralWidget);
  QGraphicsScene *Scene = new QGraphicsScene(ui->centralWidget->rect(), ui->centralWidget);
  View->setScene(Scene);
  View->installEventFilter(this);
}

MainWindow::~MainWindow()
{
  delete ui;
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
  switch(event->type())
  {
  case QEvent::MouseButtonPress:

      qDebug()<<"eaten mousepress";
      return true;
      break;
  case QEvent::MouseButtonRelease:
      qDebug()<<"eaten mouserelease";
      return true;
      break;
  default:
      //standard event processing
      return QObject::eventFilter(obj, event);
      break;
  }
}

void cDerived::mousePressEvent(QMouseEvent *event)
{
  qDebug()<<"mousepress";
  event->ignore();
}

void cDerived::mouseReleaseEvent(QMouseEvent *event)
{
  qDebug()<<"mouserelease";
  event->ignore();
}

尝试在重新实现的鼠标事件捕获器中注释掉'event-&gt; ignore()'调用,并且很清楚QGraphicsView中的mouseReleaseEvent()如何出错......

如果你可以只使用构造函数和mouseReleaseEvent(){event-&gt; ignore()}重新实现QGraphicsView作为一个非常瘦的类,那么你应该能够绕过这个bug(虽然这个hack似乎是稳定的) - 建议您的问题可能是QGraphicsView中的错误,如果没有详尽地查看QGraphicsView源代码,我们无法确定这实际上不是预期的功能,因此这个hack不会破坏任何内容!)< / p>

萨姆

答案 1 :(得分:0)

使用QGraphicsSceneMouseEvent和喜欢而不是QMouseEvent。正如我在my question中发现的那样,QGraphicsScene不会发送正常的鼠标事件。

祝你好运