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