【问题标题】:Need a Qt workaround for casting one base to the other需要一个 Qt 解决方法来将一个基础转换为另一个基础
【发布时间】: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 开发人员,但 aQRadioButtonQObject。是QLayoutItem吗?尝试在布局项上调用 widget 然后进行投射?
  • @Yakk Ha!调用widget() 并从QWidget 转换它确实可以解决它。谢谢,伙计。

标签: c++ qt multiple-inheritance dynamic-cast


【解决方案1】:

QLayoutItems 不是小部件;如果如何定位小部件(或其他东西),它们就是抽象。要获取小部件,请在布局项上调用 widget()。然后 dynamic_cast 那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    相关资源
    最近更新 更多