0

Assume that you have two classes A and B in QT C++. Your goal is to transfer data from A to B and B to A.

For A class, you have the ability to use the method findChildren. Assume that you have objects inside class A that you want to access and find out which value them have. For that, you could use:

QDoubleSpinBox *item = findChild<QDoubleSpinBox*>("analogSingleInput" + index + "MaxDoubleSpinBox");

Here we say that you want to find the objects from analogSingleInput0MaxDoubleSpinBox to analogSingleInputNMaxDoubleSpinBox where N is a large number.

I can do that here with class A:

QDoubleSpinBox *item = findChild<QDoubleSpinBox*>("analogSingleInput" + index + "MaxDoubleSpinBox");

I want to have the value from item. To access the value, I can write item->value() and it will return a double back. OK good!

Now I want to insert that value to class B. Let's say that I have created an object called b from class B. And I want to use QMetaObject to find which type of index of the method I want to use inside B.

const QMetaObject *objB = b.metaObject();
int method_index = maxObject->indexOfMethod(("setAnalogSingleInput" + index + "Max").toStdString().c_str());

Above, the the index of methods setAnalogSingleInput0Max to setAnalogSingleInputNMax can be found .

And now I want to find the method from B.

QMetaMethod method = b->method(method_index);

Now what? How can I insert item->value() into method object so the object b will have that value?

euraad
  • 2,467
  • 5
  • 30
  • 51

1 Answers1

1

Use QMetaMethod::invoke method:

method.invoke(b, Qt::DirectConnection, Q_ARG(double, item->value() ));

(where b is a pointer to instance of B class), if the obtained method returns some value, Q_RETURN_ARG is needed before passed parameters:

RetType res;
method.invoke(b, Qt::DirectConnection, Q_RETURN_ARG(RetType,res), Q_ARG(double, item->value() ));
rafix07
  • 20,001
  • 3
  • 20
  • 33