Use of Qt Property
-
I have go through
http://doc.qt.io/archives/qt-4.8/properties.html#requirements-for-declaring-propertieshttps://forum.qt.io/topic/35142/solved-what-s-the-purpose-of-the-qt-property-system
links.But I do not understand purpose of it.One is in QML. But I want to use it without QML.
Here in codeQObject *object = button; button->setDown(true); object->setProperty("down", true);
does it mean that object->setproperty() is accessing Q_PROPERTY().
-
@hjohn said in Use of Qt Property:
Is it mean that object->setproperty() is accessing Q_PROPERTY().
Yes.
-
what is the purpose of setting property using [setProperty]
Well, in this case it does not make any sense to use the property.
It may make sense if you want to execute a method in a separate thread, then you can order a queued invocation (and stop worrying about mutexes and such).
It may also make some sense if you're working with QObjects you don't know directly (you have no
#include
for them), then you can query the meta object system for all the properties.Also, it might be a way of hiding the interface - you can expose the Q_PROPERTY but keep your methods private. I don't quite see why you would do that, but you can ;-) Perhaps in connection with MEMBER properties it would work really well (then there is no need for any access methods at all == less code to write).
Plus other uses mentioned in the other thread.
As you say, though, properties make most sense in QML. In C++, you using methods directly is simpler and faster.
-
@hjohn said in Use of Qt Property:
@sierdzio
Can you link any example of proper use!You have already written the code for using properties. If you have any specific case in mind, please tell me which one.
-
@hjohn said in Use of Qt Property:
@sierdzio said in Use of Qt Property:
It may make sense if you want to execute a method in a separate thread, then you can order a queued invocation (and stop worrying about mutexes and such).
Any Example related this!!
As mentioned in the docs: https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod
QMetaObject::invokeMethod(thread, "quit", Qt::QueuedConnection);
This is similar to just connecting to the quit slot in a queue:
connect(someQObject, &SomeClass::someSignal, thread, &QThread::quit, Qt::QueuedConnection);
-
@hjohn said in Use of Qt Property:
@sierdzio How to set property of it using Q_PROPERTY()
I don't understand the question. How to set what property? What is "it" referring to?
And what has Q_PROPERTY to do with it? That macro only declares the property, it is not used to set anything.