Qt6 topics merged with General

Difference between invokeMethod and connect .

  • my app contain a lot of labels in The mainWindow Thread which updated via connect(SIGNAL(),SLOT()) from a udp thread
    (one way connection from udp to mainwindow ) , at first the app is crashing , i adedd timer in udp thread to emit update every 250ms and works ,but is slow and freezes for small time when the user make some interaction like touch on app .

    so i found another method to update GUI From Thread QMetaObject::invokeMethod

    QMetaObject::invokeMethod(label, SLOT(setText(const QString &)), Q_ARG(QString, "Hello"));
    what the diffraance betwwen 2 ways ? 
    is invoke Method not block main thread ? and completely done in the udp thread ? 

    thank you for help ,

  • Lifetime Qt Champion


    How did you implement that that thread ?
    How much data are you sending back to the GUI ?

  • @SGaist said in Diffrance between invokeMethod and connect .:

    How did you implement that that thread ?


    #include "udpthread.h"
    UdpThread::UdpThread(QObject *parent) : QObject(parent)
        qDebug() << "UDP Construction  thread:" << QThread::currentThread();
        socket = new QUdpSocket(this);     //output data Socket Work as server
        qDebug() << "bind:" << socket->bind(28000, QUdpSocket::ShareAddress);   //to send on 28000
        qDebug() << "conn:" << connect(socket, SIGNAL(readyRead()), this, SLOT(readPendingDiagrams()));
        timer = new QTimer(this);
        qDebug() << "UDP DE-Construction thread:" << QThread::currentThread();
    void UdpThread::readPendingDiagrams()
        qDebug() << "UDP RUN thread:" << QThread::currentThread();
        QByteArray Data;
        while (socket->hasPendingDatagrams()){
        QHostAddress sender;
        quint16 senderPort;
        socket->readDatagram(Data.data(), Data.size(),&sender, &senderPort);
         parseFrame(Data , Data.length());
    void UdpThread::udpdateGui()
        emit SendDataToGui(frame);
        qDebug() << "SIGNAL EMITTED FROM  THREAD" ;

    frame : puiblic QBytArray of 144 ~byte
    parseFrame : is a fuction contain heavy work

    is There adiffrance betwwen Connect and invokeMethod ?
    thanks for help.

  • Lifetime Qt Champion

    That class is not a thread.

    invokeMethod is something that you call yourself. When you use connect you setup a relation between a signal emitter and a receiver.

Log in to reply