is this possible to have a tree-like structure of QObjects and (for example) QGraphicsWidgets ? I mean, I cannot write initialization Lists like these:
class MyButton : public QGraphicsWidget
{
Q_OBJECT
public:
MyButton(int buttonId, QObject *parent = 0) : QObject(parent)
{
}
and then, be like
myButton = new MyButton(id, myObject);
Should I do .setParent or what ?
Update: see actual assignment:
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*'
}
};
I think you are confusing
parent
as in QObject parent/child hierarchy parent andparent item
in the graphics scene. If you want the former, you do need to use the usualsetParent
mechanism.Either way, that base class construction attempt is wrong. Replace it with this:
Futhermore, if you use Qt 5, you may wish to check out
Q_NULLPTR
instead of0
,NULL
,nullptr
and other variants.If you want graphics items to be parents, then you will need to use
QGraphicsObject
s that inherit bothQGraphicsItem
andQObject
. They can be used for both parenting purposes.Also, since
QGraphicsWidget
inheritsQGraphicsObject
, it can be directly for both parenting mechanisms.