c++ - QGraphicsView 和 QGraphicsItem 同时捕获的鼠标事件如何处理?

标签 c++ qt events

在这段代码中,我需要在 mousePressEvent(..) 中执行不同的操作的 ItemcustomGraphicsView .在 QGraphicsView 中重新定义鼠标事件处理程序继承的类会干扰 QGraphicsItem 的类如何分离点击的 Action Itemview以便他们可以给出不同的行为?

class CustomGraphicsView: public QGraphicsView 
{
...
protected:
    void mousePressEvent(QMouseEvent * event);
};

class Item: public QGraphicsItem
{
protected:
   void mousePressEvent(QMouseEvent * event);
};

class customWidget: public QWidget 
{
public:
customWidget(QWidget * parent = 0) ;
...
private:
    customGraphicsView * view ;
    Item * item ;
};

customWidget::customWidget(QWidget* parent)
    :QWidget(parent)
{
view = new customGraphicsView(this) ;
item = new item; 

view->addItem(item) ;

}

最佳答案

您还需要了解 QGraphicsView、QGraphicsScene 和场景中的任何项目之间的事件处理。 QGraphicsView 首先获取事件,将它们转换为场景事件,然后场景将它们传递给适当的项目。如果您没有在预期的位置看到事件,可能是因为您首先在其他地方拦截了它。我发现将调试语句放入每个事件处理程序中非常有用,这样我就可以看到事件是从哪里进来的,哪里没有。

关于c++ - QGraphicsView 和 QGraphicsItem 同时捕获的鼠标事件如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39810583/

相关文章:

mysql - 制作 MySQL 事件时出错

javascript - 调整文本区域的事件大小?

c++ - 在 C++ 生成器中向 StringGrid 添加控件

c++ - 共享内存 IPC 同步(无锁)

c++ - 有什么区别: Creating structure pointer inside a structure of the same type with the keyword struct

qt - 为 KDE 应用程序配置 Qt creator

php - 我如何将其绑定(bind)到 JQUERY 中的 onclick 事件?

c++ - 在 GDB 中调用 lambda 函数

c++ - C2143 : syntax error : missing ';' before '*'

ruby - 如何从用 qt4 编写的 ruby​​ 应用程序创建 debian 包 (*.deb)?