0

I have such a classes hierarchy:

class Base {
...
virtual QWidget* getEditor();
...
}

class Derived {
...
QWidget* getEditor() Q_DECL_OVERRIDE;
...
}

Both classes are registered via Q_DECLARE_METATYPE()

I'm getting instance of Base class from QVariant. Is it possible to get a pointer from QVariant to be able to invoke getEditor() from a Derived object?

I'm trying this atm but have no success:

if (index.data(Qt::EditRole).canConvert<Base>())
    return index.data(Qt::EditRole).value<Base>().getEditor(parent);

This snipper invokes Base class method.

Eckler
  • 61
  • 1
  • 8

1 Answers1

1

You need to make your base class' function virtual to enable polymorphism:

class Base {
...
virtual QWidget* getEditor();
...
}

class Derived {
...
QWidget* getEditor() Q_DECL_OVERRIDE;
...
}

Also, the way you have it now will cause object slicing. You will need to acquire a pointer to Derived, and call the function on that pointer.

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105