How do you use C++ Interfaces (pure virtual class) in QML
-
I am trying to make an interface that a QObject implements and assign that to a property. This is very close to the coercion example here http://doc.qt.io/qt-5/qtqml-referenceexamples-coercion-example.html . The only difference is that I don't want Person to be an actual class I want person to be an interface, defined like
class IPerson { public: virtual ~Person=default; virtual QString name()=0; virtual void setName(QString)=0; // ... etc } Q_DECLARE_INTERFACE(IPerson, "IPerson")
Is there any way to do this? I could just make a QObject and make the constructor protected, but I really would like to program to interfaces and not concrete classes if at all possible. If someone knows how to get QML to assign an object to an property who's type is a interface, I would love an example.
-
@Buttink
although I cannot rule out possible existence of some very tricky ways to achieve this, I doubt it's possible. Keep in mind that although QML is implemented in C++, its actual application is more tightly coupled with JavaScript. And in JavaScript there are no such things like interfaces or abstract classes (or classes at all). At least not natively. It's Java that has both. Some aspects of their behavior can be emulated in JavaScript but typically not without involving object instantiations of some kind. -
Hi
I dont think its possible to use a pure virtual class with QML.
I could be wrong however,
You could try to ask on the mailing list.
http://lists.qt-project.org/mailman/listinfo/development -
Hi,
This is a community driven user forum, so no they are not monitoring it.
-
@c64zottel
It is fair to say they are not very active here and it is ok to ask directly.
we do suggest that from time to time.I tried to google a bit but saw no answer regarding QML.