Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've fallen into the same trap as many do when working with threads in Qt: <a href="http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/" rel="nofollow">http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/</a>. It is <em>almost</em> always a bad idea to subclass QThread (see <a href="http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html" rel="nofollow">http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html</a> for counterexamples).</p> <p>Change your code as follows to do it the "intended" way (create a new QThread and call moveToThread on your QObject to move it to the new thread). You'll see from the output that the thread the UdpReceiver is created on is not the same as the one it receives data on, which is what you want:</p> <pre><code>#include &lt;QApplication&gt; #include &lt;QDebug&gt; #include &lt;QThread&gt; #include &lt;QUdpSocket&gt; class UdpReceiver : public QObject { Q_OBJECT private: QUdpSocket * S; int port; public: UdpReceiver(QObject* parent = 0) : QObject(parent) { qDebug() &lt;&lt; "Construction thread:" &lt;&lt; QThread::currentThreadId(); port = 9003; initialize(); } UdpReceiver(int p,QObject* parent = 0) : QObject(parent) { port = p; initialize(); } void initialize() { S = new QUdpSocket(); S-&gt;bind(port); S-&gt;connect(S,SIGNAL(readyRead()),this,SLOT(readPendingDiagrams())); qDebug() &lt;&lt; "Waiting for UDP data from port " &lt;&lt; port &lt;&lt; " ... \n"; } public slots: void readPendingDiagrams() { qDebug() &lt;&lt; "Reading thread:" &lt;&lt; QThread::currentThreadId(); while(S-&gt;waitForReadyRead()) { QByteArray datagram; datagram.resize(S-&gt;pendingDatagramSize()); QHostAddress sender; quint16 senderPort; S-&gt;readDatagram(datagram.data(), datagram.size(),&amp;sender, &amp;senderPort); qDebug() &lt;&lt; datagram.size() &lt;&lt; " bytes received .... \n"; qDebug() &lt;&lt; " bytes received .... \n"; } } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QThread *t = new QThread(); t-&gt;start(); UdpReceiver * net = new UdpReceiver(); net-&gt;moveToThread(t); return a.exec(); } #include "main.moc" </code></pre> <p>I don't have your UI code, so I don't know about any issues there. Feel free to post another question if you get stuck there and mention it in a comment and I'll try to help.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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