跳过qgraphics场景中qgraphics项的mouseEvents
skipping the mouseEvents of the qgraphicsitem in a qgraphics scene
我知道如何将事件从qgraphics场景传递到qgraphics项,但问题是该项正在执行场景的鼠标事件。
例如,在下面的代码中,当按下项目时,输出为"自定义场景已按下"
#include <QtGui>
class CustomScene : public QGraphicsScene
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(itemAt(event->pos()))
QGraphicsScene::mousePressEvent((event));
else
qDebug() << "Custom scene clicked.";
}
};
class CustomItem : public QGraphicsRectItem
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Custom item clicked.";
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomItem item;
item.setRect(20, 20, 60, 60);
CustomScene scene;
//scene().set
scene.addItem(&item);
QGraphicsView view;
view.setScene(&scene);
view.show();
return a.exec();
}
请参阅QGraphicsSceneMouseEvent::pos:的文档
返回鼠标光标在项目坐标中的位置。
这意味着,如果鼠标距离物品的左上边框10个像素,无论物品在场景中的哪个位置,您都会得到(10,10)作为坐标。
您需要的是QGraphicsSceneMouseEvent::scenePos:
返回鼠标光标在场景坐标中的位置。
将您的if
-声明更改为:
if(itemAt(event->scenePos()))
QGraphicsScene::mousePressEvent((event));
else
qDebug() << "Custom scene clicked.";
相关文章:
- 初始化具有非默认构造函数的std::数组项的更好方法
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 如何从C++端挂接到 QML 项的 onClick 事件
- 数组初始值设定项的构造函数歧义
- C++17:如何在并行 STL 中获取工作项的索引
- 传递空初始值设定项列表时使用右值和左值引用候选项的重载解析
- C++ 替换字节数组中项的顺序
- 在 CPP 中打印具有重复项的选定长度字符的所有排列
- 列表视图项的替代颜色
- 清理"QSettings"注册表项的最佳方法(Windows上的Qt 5)
- 使用迭代器替换映射中的常量项的方法
- 当键是虚拟继承中涉及的基类指针时,对 std::unordered_map 项的访问崩溃
- 访问类成员向量最后一项的正确方法
- 在 CMAKE 中使用具有静态依赖项的库
- 将指向多维数组向量中的单个项的指针传递给函数 c++
- 获取 c++ mfc vs2015 中弹出资源菜单项的 id
- 按升序打印矢量的所有元素直到它为空而没有重复项的最有效方法是什么?
- 在位置上运行具有依赖项的.exe文件
- QGraphics项目项更改函数与项的状态不匹配?
- 跳过qgraphics场景中qgraphics项的mouseEvents