Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

    Hi,

    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 ?

    Hi,

    #include "udpthread.h"
    
    UdpThread::UdpThread(QObject *parent) : QObject(parent)
    {
    
        //constractor
        qDebug() << "UDP Construction  thread:" << QThread::currentThread();
        //intialize
        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);
        connect(timer,SIGNAL(timeout()),this,SLOT(udpdateGui()));
        timer->start(250);
    }
    
    UdpThread::~UdpThread()
    {
        qDebug() << "UDP DE-Construction thread:" << QThread::currentThread();
    
    }
    
    
    void UdpThread::readPendingDiagrams()
    {
    
        qDebug() << "UDP RUN thread:" << QThread::currentThread();
    
    
        QByteArray Data;
        while (socket->hasPendingDatagrams()){
        Data.resize(socket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;
        socket->readDatagram(Data.data(), Data.size(),&sender, &senderPort);
    
    }
    
         parseFrame(Data , Data.length());
         Data.clear();
    
    }
    
    
    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