Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare New-Signal-Slot syntax in Qt 5 as a parameter to function

How can I pass signal or slot (member-function, new syntax in Qt 5) as a parameter to function and then call connect?

e.g. I want to write a function that waits for a signal.

Note: It is not compile - PointerToMemberFunction is my question.

bool waitForSignal(const QObject* sender, PointerToMemberFunction??? signal, int timeOut = 5000/*ms*/)
{
  if (sender == nullptr)
    return true;
  bool isTimeOut = false;
  QEventLoop loop;
  QTimer timer;
  timer.setSingleShot(true);
  QObject::connect(&timer, &QTimer::timeout,
    [&loop, &isTimeOut]()
    {
      loop.quit();
      isTimeOut = true;
    });
  timer.start(timeOut);
  QObject::connect(sender, signal, &loop, &QEventLoop::quit);
  loop.exec();
  timer.stop();
  return !isTimeOut;
}

Is there any way to pass list of signals to this function for connection?

like image 407
A.Danesh Avatar asked Jan 03 '15 15:01

A.Danesh


1 Answers

You should create template:

template<typename Func>
void waitForSignal(const typename QtPrivate::FunctionPointer<Func>::Object *sender, Func signal) {
    QEventLoop loop;
    connect(sender, signal, &loop, &QEventLoop::quit);
    loop.exec();
}

Usage:

waitForSignal(button, &QPushButton::clicked);
like image 119
Meefte Avatar answered Sep 16 '22 22:09

Meefte