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

你。 提交于 2019-11-29 13:52:38

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);

You can simply use QSignalSpy to wait for a signal to be emitted by :

QSignalSpy spy(sender, SIGNAL(someSignal()));
spy.wait(timeOut);

Or (This is possible in Qt 5.4) :

QSignalSpy spy(sender, &SomeObject::someSignal);
spy.wait(timeOut);

If you want to implement it in a function :

bool waitForSignal(const typename QtPrivate::FunctionPointer<Func>::Object *sender, Func signal, int timeOut = 5000/*ms*/)
{
   QSignalSpy spy(sender, signal);
   return spy.wait(timeOut);
}

Do not forget to add the relevant module in qmake :

QT += testlib
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!