Home > Blockchain >  Q_INVOKABLE on all getters and setters functions inside QT?
Q_INVOKABLE on all getters and setters functions inside QT?

Time:02-04

I have some fields in my class. I want to have Q_PROPERTY on all these fields and also have getters and setters.

Example for the private field float pwm7Min, I can get my getters and setters and also property using this code.

Q_PROPERTY(float pwm7Min READ getPwm7Min WRITE setPwm7Min NOTIFY pwm7MinChanged)

This is generated by automatic in QT.

enter image description here

Question:

But I want to Q_INVOKABLE all the setters and getters. Is there a way to do that in QT by using QT Designer, or do I have to manually implement them by my self?

That's because I have got this error and the solution is Q_INVOKABLE. But my .cpp file is over 1000 lines of code and only contains getters and setters.

 QMetaObject::invokeMethod: No such method IOcalibration::setInputCapture0Max(float)

It have origins from this problem: QMetaObject::invokeMethod: No such method when using inheritance

CodePudding user response:

If you declare a Q_PROPERTY you can access the object through the property name in QML, you don't need to declare any setter or getter as Q_INVOKABLE, because the property it's already registered in the meta object system.

if you have

Q_PROPERTY(float pwm7Min READ getPwm7Min WRITE setPwm7Min NOTIFY pwm7MinChanged)

then just access in qml via

pwm7Min = 0.5

use setter/getter there is just wrong, that's why Qt Creator does not provide that option.

CodePudding user response:

You can only invoke methods known by the Qt meta object system with QMetaObject::invokeMethod, i.e. slot functions and Q_INVOKABLE functions.

Easiest way to register your getters and setters to moc is to add a separate public scope with the keyword slots for them in your header file:

public slots:
    void setInputCapture0Max(float inputCapture0Max);
  •  Tags:  
  • Related