QGraphicsWidget as a child of QObject

231 views Asked by At

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*'
    }
};
1

There are 1 answers

2
László Papp On BEST ANSWER

I think you are confusing parent as in QObject parent/child hierarchy parent and parent item in the graphics scene. If you want the former, you do need to use the usual setParent mechanism.

Either way, that base class construction attempt is wrong. Replace it with this:

MyButton(int buttonId, QGraphicsItem *parent = 0) : QGraphicsWidget(parent)

Futhermore, if you use Qt 5, you may wish to check outQ_NULLPTR instead of 0, NULL, nullptr and other variants.

If you want graphics items to be parents, then you will need to use QGraphicsObjects that inherit both QGraphicsItem and QObject. They can be used for both parenting purposes.

Also, since QGraphicsWidget inherits QGraphicsObject, it can be directly for both parenting mechanisms.