Skip to content

How to connect to a signal with specific argument? #59

@qiangxinglin

Description

@qiangxinglin

We can connect to a Qt signal by using

// [signal] Object::finished(const QString&)
auto output = QtPromise::connect(obj, &Object::finished)
    .then([](const QString& data) {
        // use the data here to do further work
    });

How to implement a promise only resolved when not only the signal emitted but also meet a specific condition?

// task with int task_id
QPromise<int> task = {...}

// [signal] Object::finished(const int&)
task.then([](const int& task_id){
  QObject* context = new QObject(this);
  return QtPromise::QPromise<Qstring>{[=](const auto& resolve, const auto& reject) {
      QObject::connect(this, &Object::finished, context, [=](const int& finshed_id) {
          if (task_id == finished_id)
          {
              resolve("task{} is finished, we can proceed to next", task_id);
              delete context;
          }
      });
  })

Would this work? Any elegant solution to handle the context deletion (for disconnect the lambda expression)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions