Disconnecting lambda functions in Qt5

前端 未结 3 2016
攒了一身酷
攒了一身酷 2020-12-08 09:35

Is it possible to disconnect a lambda function? And if \"yes\", how?

According to https://qt-project.org/wiki/New_Signal_Slot_Syntax I need to use a QMetaObjec

3条回答
  •  既然无缘
    2020-12-08 10:19

    The context solution from ecatmur's answer is the easiest option, but I think the use of the smart pointer makes it harder to understand. I'd use a raw pointer instead:

    QObject *context = new QObject(this);
    connect(sender, &Sender::signal, context, [context] {
      delete context;
      // ...
    });
    

提交回复
热议问题