Results 1 to 10 of 10

Thread: QThread, signalling and readyRead()

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread, signalling and readyRead()

    I think you are going for the whole thing wrong First you are assuming that by the time you receive readyRead() all the data you need will be available, which is incorrect, only part of it may be there. And to overcome this issue you should read the data from the port when it comes in and only when there is enough to process it, signal the wait condition. This way you'll get rid of two problems at once. And no need to disconnect signals.

    Next thing - by groupping everything into one object, your program has become everything but a good designed object oriented application. Instead of aggregating, separate the data used by different tasks. Make them two classes containing only the data they need. There is a question whether you need the threads at all - from what I see, you don't, you can do everything in one thread. Once you have clear design, everything will become easier.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default Re: QThread, signalling and readyRead()

    Part of the reason I feel totally lost is that I'm migrating from pthreads, where I don't need to create a separate class for threads, and where I can run things concurrently in a way that seems coherent. If I have different devices that need to talk to each other and to my application, I would think that threads would be involved.

    I don't understand how I can emit a signal only when there are enough bytes available to indicate a proper response from my device.

    I don't understand how I could run the GUI, and several devices that all interact which each other with a single thread. I couldn't even run the GUI by itself doing mundane operations like opening up a dialog box without multiple threads. So could you please point me in the right direction?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread, signalling and readyRead()

    Quote Originally Posted by kachofool View Post
    Part of the reason I feel totally lost is that I'm migrating from pthreads, where I don't need to create a separate class for threads,
    Probably because pthreads has a C interface

    and where I can run things concurrently in a way that seems coherent.
    I don't think this has anything to do with your current situation.

    If I have different devices that need to talk to each other and to my application, I would think that threads would be involved.
    Only if i/o operations are blocking. Otherwise you wouldn't need threads.

    I don't understand how I can emit a signal only when there are enough bytes available to indicate a proper response from my device.
    I didn't say you should. I said you should react on readyRead() in a different manner than you are doing now. Buffer the data until there is enough to perform your task. Then you can emit a signal or do whatever you like.

    For instance assuming a "datagram" would be a complete line of text, you could do this in your slot connected to readyRead():
    Qt Code:
    1. void Class::onReadyRead() {
    2. while(device->canReadLine()) {
    3. QByteArray data = device->readLine();
    4. doSomethingWithThe(data);
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 


    So could you please point me in the right direction?
    Signals and slots are your friends.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default Re: QThread, signalling and readyRead()

    My 'class::OnReadyRead' never runs to completion -- that's the first problem.

    The second issue is I don't understand how to set up the structure of my program using Qt. I have i/o going on with several devices. The i/o uses software 'handshaking', in that I talk to my device, wait for a response, verify the response (take corrective action if necessary), rinse and repeat. The thread communicating to the device should block while waiting for a response (so in this sense there is blocking involved).

    So I'm really trying to figure out what method to set this sort of system up in. What I did initially was set up threads to talk to my devices, and block them using wait conditions after they had sent something to the device. I then tried to use signals (to and from the main application thread) to tell these threads to unblock, but this didn't work.

    When you say 'signals and slots are your friend' could you please emphasize in the context of what I'm trying to accomplish? I really appreciate the help so far.

    -KF

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.