0

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

euraad
  • 2,467
  • 5
  • 30
  • 51

2 Answers2

0

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.

Moia
  • 2,216
  • 1
  • 12
  • 34
  • 1
    To make this work don't forget to add your class to qml context. ```QQmlEngine engine; MyClass myClass; QQmlContext *context = new QQmlContext(engine.rootContext()); context->setContextProperty("myClass", &myClass);``` Than `myClass.pwm7Min = 0.5` – Mike Feb 03 '22 at 13:21
  • QML seems to be difficult. I stick to C++. No when I apply `Q_PROPERTY`, I still got my error message `QMetaObject::invokeMethod: No such method ` – euraad Feb 03 '22 at 18:32
  • Because are you calling sometghing like setPwm7Min(x). You should show your QML because it's there your issie, but that is another question you should ask. – Moia Feb 04 '22 at 08:31
0

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);
talamaki
  • 5,324
  • 1
  • 27
  • 40
  • OP said he's already using a property, so there's already a setter, but it's not the way it should be called from QML. He is aware he sould expose method to QML tha'ts why he would use Q_INVOKABLE, but it's not the way it solves his issue. – Moia Feb 04 '22 at 08:33
  • @Moia OP is not saying they have problems with the property from QML side, instead that they can't call the setter from C++ with QMetaObject::invokeMethod. That can be solved by making sure the setter is also a slot function. – talamaki Feb 04 '22 at 12:35