c++ - 继承QGraphicsItem+QWidget时GUI冲突

标签 c++ qt qwidget qgraphicsitem

我正在编写一个继承自 QGraphicsItem 和 QWidget 的类。我这样做的原因是因为我想利用 setPos() 和 setRotation()(来自 QGraphicsItem)使其可拖动和 customContextMenuRequested 信号(来自 QWidget)。代码如下所示:

class MyClass: public QWidget, public QGraphicsItem {    
  Q_OBJECT 
/* Variables and Methods here */ 
};

我不是 QT 的专家,也不是它在每个细节方面的专家。但是,通过测试代码,我感觉 MyClass 实际上由两个可能具有不同位置的 GUI 元素组成。问题是当我将它们放在彼此之上时,我要么无法使上下文菜单工作,要么无法拖动它。所以我假设在顶部只能有两者之一(它接收鼠标点击事件)。 MyClass 是作为 QGraphicsItem 的 QGraphicsScene,并且 QGraphicsScene 被设置为 QGraphicsView,如果这是相关的。

我的假设是否正确?

处理这个问题的优雅方式是什么?

提前致谢!

编辑:如果有帮助,我的应用程序基本上应该这样做。 用户应该能够将多个 MyClass 对象和类似对象添加到场景中并自由移动/旋转它们。

最佳答案

QWidgetQGraphicsItem 在 Qt 中是根本不同的东西。从文档 QGraphicsItem 是:

The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.

It provides a light-weight foundation for writing your own custom items. This includes defining the item's geometry, collision detection, its painting implementation and item interaction through its event handlers. QGraphicsItem is part of the Graphics View Framework

基本上它只用于QGraphcisScene

如果您需要一个可从 QWidget 获得的自定义上下文菜单,我会在自定义 QGraphicsView 中实现它,它(最终)继承自 QWidgetQGraphicsView 可用于显示 QGraphicsScene,它将保存您的自定义 QGraphicsItem

关于c++ - 继承QGraphicsItem+QWidget时GUI冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49449580/

相关文章:

c++ - QStringList::append() 性能不佳

c++ - 扩展公共(public)基础 : Diamond inheritance vs. QObject

c++ - C++ 中的自动变量

c++ - Visual Studio 2010 像 Visual Studio 6 一样在 C++ 中编译内联程序集?

c++ - 如何只将新文本写入文本文件

c++ - Qt 小部件内存管理

c++ - 将自定义参数传递给 Qt/C++ 中的插槽

c++ - 从 QGridLayout 中删除 QWidgets

c++ - 输出 char8_t const* 到 cout 和 wcout,一编译一不

c++ - 从较小类型的指针定义的范围构造 vector