简体   繁体   中英

Reverse QMetaMethod::fromSignal()

We can have

QMetaMethod::fromSignal(PointerToMemberFunction)

returning QMetaMethod

My question is, is it possible to have something like this?

fromMetaMethod(QMetaMethod)

returning PointerToMemberFunction ??

Thanks.

One way to create the connection you want does not require you to find any methods address.
Simply:

  1. Create a signal-to-signal connection, from SomeClass::someSignal to your own signal in your own class using the SIGNAL() connection style.
  2. Connect your signal to the lambda.

You only have to make sure the parameters of the original signal are forwarded to your lambda.

Example:

QObject::connect(
    pointerToSomeClass, SIGNAL(someSignal(int)),
    pointerToMyClass  , SIGNAL(mySignal(int)),
    Qt::DirectConnection
);
QObject::connect(
    pointerToMyClass, &MyClass::mySignal,
    [](int i) { ... }
);

If you happen to need the sender of the original signal, then you will need a slot.

Example:

void myClass::mySlot(int i)
{
    emit mySignal(sender(), i);
}

and

QObject::connect(
    pointerToSomeClass, SIGNAL(someSignal(int)),
    pointerToMyClass  , SIGNAL(mySlot(int)),
    Qt::DirectConnection
);
QObject::connect(
    pointerToMyClass, &MyClass::mySignal,
    [](QObject* sender, int i) { ... }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM