Q_PROPERTY
-
Hello I want to use a variable from .qml file in a .cpp file. I want to do something according to value of (which is in .qml file) isCamRezistans in C++. So I want to put it's value into m_isCamRezistans. For that I use Q_PROPERTY as follows:
class CAN : public QObject { Q_OBJECT public: Q_PROPERTY(quint32 bitrate MEMBER m_bitrate WRITE setBitrate NOTIFY bitrateChanged) Q_PROPERTY(bool isCamRezistans MEMBER m_isCamRezistans READ m_isCamRezistans) private: quint32 bitrate; bool m_isCamRezistansQ_PROPERTY(quint32 bitrate MEMBER m_bitrate WRITE setBitrate NOTIFY bitrateChanged) does not give any errors. However, Q_PROPERTY(bool isCamRezistans MEMBER m_isCamRezistans READ m_isCamRezistans)
gives "error: expression cannot be used as a function." How can I solve that? Also do I need to use NOTIFY and signal slot mechanism in here? Or once I use Q_PROPERTY and READ, will the value be automatically updated? -
Hello I want to use a variable from .qml file in a .cpp file. I want to do something according to value of (which is in .qml file) isCamRezistans in C++. So I want to put it's value into m_isCamRezistans. For that I use Q_PROPERTY as follows:
class CAN : public QObject { Q_OBJECT public: Q_PROPERTY(quint32 bitrate MEMBER m_bitrate WRITE setBitrate NOTIFY bitrateChanged) Q_PROPERTY(bool isCamRezistans MEMBER m_isCamRezistans READ m_isCamRezistans) private: quint32 bitrate; bool m_isCamRezistansQ_PROPERTY(quint32 bitrate MEMBER m_bitrate WRITE setBitrate NOTIFY bitrateChanged) does not give any errors. However, Q_PROPERTY(bool isCamRezistans MEMBER m_isCamRezistans READ m_isCamRezistans)
gives "error: expression cannot be used as a function." How can I solve that? Also do I need to use NOTIFY and signal slot mechanism in here? Or once I use Q_PROPERTY and READ, will the value be automatically updated?@Gunkut said in Q_PROPERTY:
READ m_isCamRezistans
READ requires a function not a variable, see https://doc.qt.io/qt-5/properties.html
-
Okay so is there an easier way to get values of qml variables. Because I will have many variables and writing a function for each of them looks inefficient.
-
Okay so is there an easier way to get values of qml variables. Because I will have many variables and writing a function for each of them looks inefficient.
-
@Gunkut From the documentation I linked: "A READ accessor function is required if no MEMBER variable was specified". So, you don't have to provide READ at all.
@jsulm I see in that case can I only use MEMBER and not NOTIFY? Would a change in qml variable directly change the member variable? Such as using:
Q_PROPERTY(QString text MEMBER m_text )instead of :
Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)