sigil
sigil

Reputation: 825

Need a Qt workaround for casting one base to the other

Consider the following:

class MyInterface { /* ... */ }; // has virtual methods and all

class MyToolButton : public QToolButton, public MyInterface { /* ... */ };

class MyRadioButton : public QRadioButton, public MyInterface { /* ... */ };

class MyFrame : public QFrame { /* ... */ };

void MyFrame::doesNotWork()
{
    for(int i = 0; i < layout()->count(); ++i)
    {
        QLayoutItem *item = layout()->itemAt(i); // can be either MyToolButton or MyRadioButton
        Q_ASSERT(item); // passes
        MyInterface *interface = dynamic_cast<MyInterface*>(item);
        Q_ASSERT(interface); // TRIGGERS
    }
}

Is there some creative Qt way to get a pointer to MyInterface here? QLayoutItem does not inherit from QObject, which is sort of sad.

Upvotes: 1

Views: 50

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275395

QLayoutItems are not the widgets; they are the abstraction if how a widget (or something else) is positioned. To get the widget, call widget() on the layout item. Then dynamic_cast that.

Upvotes: 4

Related Questions