How to execute a functor or a lambda in a given thread in Qt, GCD-style?

后端 未结 5 1510
萌比男神i
萌比男神i 2020-11-22 05:11

In ObjC with GCD, there is a way of executing a lambda in any of the threads that spin an event loop. For example:

dispatch_sync(dispatch_get_main_queue(), ^         


        
5条回答
  •  太阳男子
    2020-11-22 05:51

    Others have exelent answers. here is my suggest. instead of doing something like this:-

    QMetaObject::invokeMethod(socketManager,"newSocket",
                              Qt::QueuedConnection,
                              Q_ARG(QString, host),
                              Q_ARG(quint16, port.toUShort()),
                              Q_ARG(QString, username),
                              Q_ARG(QString, passhash)
                              );
    

    do something like this which is more nice:-

    QMetaObject::invokeMethod(socketManager,[=](){
        socketManager->newSocket(host,port.toUShort(),username,passhash);
    },Qt::QueuedConnection);
    

提交回复
热议问题