Note that there are some explanatory texts on larger screens.

plurals
  1. PONetworking without blocking ui in Qt 4.7
    primarykey
    data
    text
    <p>I have a server to which multiple clients can connect to. The client is GUI while the server is command line. The client has several functions (such as connect and login) which, when sent to the server should receive a reply.</p> <p>Basically I need to run the QTcpSocket functions waitForConnection and waitForReadyRead. However, I need to do this without blocking the UI.</p> <p>What I thought of doing was the following: Have a class (Client) implement QThread which does all the waiting. This is created in main.</p> <pre><code>Client::Client (...) { moveToThread (this); // Not too sure what this does mClient = new QTcpSocket (this); start(); } void Client::run (void) { exec(); } void Client::connectToServer (...) { mClient-&gt;connectToHost (hostname, port); bool status = mClient-&gt;waitForConnected (TIMEOUT); emit connected (status); } void Client::login (...) { ... Similar to connectToServer ... } </code></pre> <p>Then the GUI (for example, ConnectToServerDialog) I run this whenever I am ready to make a connection. I connect the "connected signal" from the thread to the dialog so when I am connected or connection timed out it will emit this signal.</p> <pre><code>QMetaObject::invokeMethod (mClient, "connectToServer", Qt::QueuedConnection, Q_ARG (const QString &amp;, hostname), Q_ARG (quint16, port)); </code></pre> <p>I am getting an assert failure with this (Cannot send events to objects owned by a different thread.) Since I am fairly new to Qt I don't know if what I am doing is the correct thing.</p> <p>Can somebody tell me if what I am doing is a good approach and if so why is my program crashing?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload