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
parentas in QObject parent/child hierarchy parent andparent itemin the graphics scene. If you want the former, you do need to use the usualsetParentmechanism.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_NULLPTRinstead of0,NULL,nullptrand other variants.If you want graphics items to be parents, then you will need to use
QGraphicsObjects that inherit bothQGraphicsItemandQObject. They can be used for both parenting purposes.Also, since
QGraphicsWidgetinheritsQGraphicsObject, it can be directly for both parenting mechanisms.