1
考虑以下几点:需要一个Qt解决方法铸造一个基地到另一个
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开发者,但是'QadioButton'是'QObject'。它是一个'QLayoutItem'吗?尝试调用布局项目上的“小部件”,然后投射? – Yakk
@Yakk哈!调用'widget()'并从'QWidget'强制转换的确可以解决它。谢啦。 – sigil