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

QThread parent issues



  • Hello, i have a issue rigth now about threads i have a socket thread which own the socket, but when i invoke the send methoed from the gui thread and then call write on the socket i get the following error message.
    (Parent is QNativeSocketEngine(0x26123e4ada0), parent's thread is SocketThread(0x26123e3bda0), current thread is QThread(0x2611f38fde0)
    but i invoked the methoed should the current thread not be socketthread and not qthread?
    How do i fix this?


  • Lifetime Qt Champion

    Hi,

    If you call it directly then no.

    How are you using that code ?



  • i call it like so

    QMetaObject::invokeMethod(clients[0], "send", Qt::QueuedConnection, Q_ARG(Message, message2)); 
    

    this from my gui thread, clients[0] is a pointer to the sockethread and send is the send methoed.


  • Lifetime Qt Champion

    Can you show the code where you create your objects and thread ?


  • Moderators

    @raketmus said in QThread parrent issues:

    QMetaObject::invokeMethod(clients[0], "send", Qt::QueuedConnection, Q_ARG(Message, message2)); 
    

    ...clients[0] is a pointer to the sockethread and send is the send methoed.

    The first argument to QMetaObject::invokeMethod() should be an object that lives in the thread. You should pass a pointer to an object that was created inside the thread.

    client[0] manages a thread, but it does not live in that thread. It lives in your GUI thread. Call this to check:

    qDebug() << clients[0];
    qDebug() << clients[0]->thread();
    qDebug() << qApp->thread(); // qApp is your QApplication which lives in the GUI thread
    

    i have a socket thread

    You don't usually need a thread for a socket. Just leave the socket in the GUI thread. Qt socket classes and other communications classes are asynchronous.



  • @JKSH
    Here is the output of the debugging it.

    SocketThread(0x1d02b90b3d0)
    QThread(0x1d02b870250)
    QThread(0x1d02b870250)
    

    It would see they all have the same id.

    You don't usually need a thread for a socket. Just leave the socket in the GUI thread. Qt socket classes and other communications classes are asynchronous.

    i am running a server and i would like to use blocking since that is how i set my sockets up.
    but i am not sure how you want me to call it if not like i am doing right now.


Log in to reply