【发布时间】:2017-04-17 21:52:59
【问题描述】:
考虑以下几点:
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
}
}
是否有一些创造性的 Qt 方法可以在此处获取指向 MyInterface 的指针? QLayoutItem 没有继承自 QObject,这有点可悲。
【问题讨论】:
-
这里不是 Qt 开发人员,但 a
QRadioButton是QObject。是QLayoutItem吗?尝试在布局项上调用widget然后进行投射? -
@Yakk Ha!调用
widget()并从QWidget转换它确实可以解决它。谢谢,伙计。
标签: c++ qt multiple-inheritance dynamic-cast