需要一个 Qt 解决方法来将一个基础转换为另一个基础
Need a Qt workaround for casting one base to the other
考虑以下几点:
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
,这有点可悲。
QLayoutItems 不是小部件;如果小部件(或其他东西)如何定位,它们就是抽象。要获取小部件,请在布局项上调用 widget()
。然后dynamic_cast那个。
考虑以下几点:
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
,这有点可悲。
QLayoutItems 不是小部件;如果小部件(或其他东西)如何定位,它们就是抽象。要获取小部件,请在布局项上调用 widget()
。然后dynamic_cast那个。