Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QUdpSocket and QThread
Forum Updated to NodeBB v4.3 + New Features

QUdpSocket and QThread

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blackbelt
    wrote on last edited by
    #1

    Hello guys.

    In my application I need to wait for event on a UDP socket. How can I achive this? Now I had:

    subclassed Thread and ovveride run()

    call bind(QHostAddress, int)

    call wait on a QWaitCondition

    @void MyThread::run()
    {
    bool connected = m_socket->bind(QHostAddress::LocalHost, LOCALPORT);

    while (true)
    {
    if (exit)
    return;

    noDataAvailable.wait(&mutex);

    }
    }@

    In MyThread constructor I also connect to readyRead signal

    @QObject::connect(m_socket, SIGNAL(readyRead()), this, SLOT(readData()));@

    My questions are:

    1. Need I really a QWaitCondition (since I want MyThread processes info I think yes)
    2. Who have to be responsible to wake MyThread? More specifically how do I understand if data are available?

    I am really confuse. Could somebody help me ?

    ty

    1 Reply Last reply
    0
    • B Offline
      B Offline
      blackbelt
      wrote on last edited by
      #2

      ok. As far as I understand the Q*Socket are async but, still, reading from the socket is performed on the UI thread when the signal readyRead is emitted. Is that correct? Is that safe from the performance perspective? Should I read the socket's content in another thread?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        If you want a signal/slot to be emitted in a thread from an object, you must be in an execution loop...therefore, you being in a while loop infinitely makes it so that you will never get a signal from the socket the way that you have it.

        @
        void MyThread::run()
        {
        QUdpSocket m_socket;
        m_socket.bind(QHostAddress::LocalHost, LOCALPORT);
        connect(&m_socket, SIGNAL(readyRead()), this, SLOT(readData()));
        this->exec();
        }

        void MyThread::readData()
        {
        // data received, emit signal to main gui thread with the data...or do what ever you want to do
        }
        @

        There isn't any error checking...but you should get your signal if data is received. The exec() sets the thread in an execution loop and allows the thread to receive signals from objects created within the thread. You can pop out of the execution loop by calling exit() from your main thread.

        Read the docs on the QThreads:
        http://qt-project.org/doc/qt-5.0/qthread.html

        If you really want it to be in a while loop, you also have the other option to use a state machine in which calls waitForReadyRead() to check for data and then manipulate accordingly without connecting any signals and slots

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          Also "You're doing it wrong":http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/ read this article for "recommended use of QThread":http://qt-project.org/wiki/QThreads_general_usage. With the recommended approach many common mistakes especially those affecting signals and slots can be avoided.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            blackbelt
            wrote on last edited by
            #5

            I had reading somewhere that moveToThread(this) was not more necessary since Qt 4.7. Thank you both. I will go deeply in the docs you had provided. ty again

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved