QGraphicsWidget 作为 QObject 的子项
QGraphicsWidget as a child of QObject
这是否有可能具有QObjects和(例如)QGraphicsWidgets的树状结构?我的意思是,我不能像这样编写初始化列表:
class MyButton : public QGraphicsWidget
{
Q_OBJECT
public:
MyButton(int buttonId, QObject *parent = 0) : QObject(parent)
{
}
然后,像
myButton = new MyButton(id, myObject);
我应该做.setParent还是什么?
更新:查看实际分配:
class myObject : public QObject
{
Q_OBJECT
public:
MyObject(QObject *parent = 0) : QObject(parent) {
}
MyButton *myButton;
void showButton(){
myButton = new MyButton(id, this); //no matching function for call to 'MyButton::MyButton(MyObject* const)'
//note: no known conversion for argument from 'MyObject* const' to 'QGraphicsObject*'
}
};
我认为您在图形场景中混淆了 QObject 父/子层次结构父级和parent item
parent
。如果你想要前者,你确实需要使用通常的setParent
机制。
无论哪种方式,基类构造尝试都是错误的。将其替换为:
MyButton(int buttonId, QGraphicsItem *parent = 0) : QGraphicsWidget(parent)
此外,如果您使用Qt 5,您可能希望签出Q_NULLPTR
而不是0
,NULL
,nullptr
和其他变体。
如果希望图形项成为父项,则需要使用同时继承QGraphicsItem
和QObject
的QGraphicsObject
。它们可以用于育儿目的。
此外,由于QGraphicsWidget
继承QGraphicsObject
,因此可以直接用于两种育儿机制。
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将Ref对象作为类成员
- 虚拟决赛作为安全
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 使用指向成员的指针将成员函数作为参数传递
- 我应该使用什么来代替void作为变体中的替代类型之一
- 何时提供默认参数作为模板参数
- 如何将 I->getType() 作为参数传递给 llvm 中的 CreateCall?
- C++使用数组作为多维数组,尽管将其初始化为带有指针的 1D
- C++匿名结构作为std::映射值
- C++:如何使函数只返回作为列表一部分的字符串
- 智能指针作为 QObject::d eleteLater() 的替代品
- 将QMETAMETHOD作为参数传递到使用新的Qobject :: Connect语法的函数
- QwidgetWindow作为QT5的事件过滤器中的QObject
- 使用 QList<QObject*> 作为模型的 QML ListView
- 强制QObject作为QWidget的父级的后果是什么?
- 在传递的对象即将被销毁之前,发出将QObject指针作为参数传递的信号是否安全
- QGraphicsWidget 作为 QObject 的子项
- Qt 在构建移动到线程的 QObject 时将"this"作为父传递