K
K
Kalombyr2018-01-14 09:48:39
Qt
Kalombyr, 2018-01-14 09:48:39

How to work in QML through a class interface?

Good afternoon.
There is a great need to transfer the class interface to QML and work through it there, mainly calling slots.
The matter is that the interface should not necessarily be inherited from QObject, have macro Q_OBJECT/Q_GADGET.
It is not necessary to have a "clean" interface; I understand that without any concessions, it will not work out as you want. already tried Q_DECLARE_METATYPE and so on - does not want to.
Tell me, please, how is it right?

IInterface  * f = new MyClass(0);
rootObj.setProperty("classInterface", QVariantFromValue(f));

....
property var classInterface: null;
....
onClick: {
   console.log( classInterface.publicSlotTestFunction()  );
}
...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel K, 2018-01-16
@Kalombyr

In general, this is purely theoretically possible, but with limitations due to the principle of the Qt metasystem, namely, it registers each type with its own number, and then, when necessary, creates an instance of the class based on this number.
That is, you can replace it so that when creating an interface, create the class itself instead, but then you will not be able to use the interface "in its direct" sense, i.e. use multiple inheritance, i.e. one class, one interface is all. Although it may even be possible to get around this if you delve into the metasystem well. I didn’t look too closely at the insides, but what is possible is for sure.
UPDT1: Sorry, everything has already been invented before us: pavelk.ru/ispolzovanie-interfejsov-klassov-v-qt-i-qml

T
tschin, 2018-01-14
@tschin

qmlRegisterType("my.class",1,0,"Class") might help you, but if you want class properties to be visible from qml, you can't do without QObject inheritance.
Also, in addition to Q_DECLARE_METATYPE, you need to do qRegisterMetaType

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question