Results 1 to 3 of 3

Thread: Problem quitting QThread that uses readLine

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Problem quitting QThread that uses readLine

    I have a thread I run in my console app that allows the user to quit at any time by typing 'q' and pressing enter. The thread gracefully ends when the user does this because it breaks out of my loop and the run() function terminates. However, there are certain other situations in which I want to close the app (e.g. some event occurs), but I can't terminate the thread because it is always blocked on the readLine() call so I get a warning message upon quitting the app saying something like "Quit application while QThread still running..."

    Is there a way around this to quit gracefully and not receive this warning?

    Qt Code:
    1. class ConsoleReader : public QThread
    2. {
    3. public:
    4. ConsoleReader( QObject * parent = 0 ) : QThread( parent )
    5. {
    6. QThread::setTerminationEnabled( true );
    7. }
    8. virtual void run()
    9. {
    10. QTextStream qin( stdin, QFile::ReadOnly );
    11. forever
    12. {
    13. QString line = qin.readLine();
    14. if( !line.isNull() )
    15. {
    16. if( line.toLower() == "q" )
    17. {
    18. break;
    19. }
    20. }
    21. }
    22. }
    23. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2010
    Posts
    73
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 8 Times in 8 Posts

    Default Re: Problem quitting QThread that uses readLine

    I have not looked at the source code for readLine(), but, typically, the code waits until either a new line has been entered, or until the stream ends (ie, the user presses Ctrl+Z I think). In your case, neither occurs when the program ends, so this thread is left hanging I suppose.

    When I do this sort of thing, I usually have a method such as isEndRequested and requestEnd. I implement this to be far more polite than the terminate slot in the QT thread class. Terminate just kills things regardless of what they are doing. In your code, when the program is requested to quit, I would then call requestEnd, and then wait for it to finish. If you do this, however, then your run method must notice and end quickly. Your run method would no longer use "forever", but instead, something more like this:
    Qt Code:
    1. while (!isEndRequested()
    2. {
    3. if (data is available from the stream)
    4. {
    5. read_from_stream
    6. }
    7. else
    8. {
    9. sleep a bit
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    The QIODevice has methods available that can help. Things like bytesAvailable and watForReadyRead. The later will allow you to skip a thread sleep. The most difficult part here is that you must then accumulate the characters and process out lines by yourself.

  3. #3
    Join Date
    Jan 2010
    Posts
    73
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 8 Times in 8 Posts

    Default Re: Problem quitting QThread that uses readLine

    I must admit. I have lost a lot of time trying to read directly from stdin in a simple Console application, all to no affect. My expectation was that I would be able to connect directly using a QFile using something like:
    Qt Code:
    1. f.open(0, QFile::ReadOnly);
    To copy to clipboard, switch view to plain text mode 
    I failed just as horribly trying to use a text stream and then hitting the device directly from the stream. I have usually done this by going directly to the keyboard object, but this is very much NOT QT specific.
    This claims to be a working example, that may work for you, but did not function in the context of my simple console application with no event loop and such
    http://lists.kde.org/?l=kde-bindings...3495705263&w=2

Similar Threads

  1. readLine in QProcess
    By grisson in forum Qt Programming
    Replies: 7
    Last Post: 15th October 2009, 17:39
  2. Quitting a QT app
    By Aztral in forum Newbie
    Replies: 4
    Last Post: 21st August 2009, 19:09
  3. Qtextstream::readline()
    By dawn_to_dusk_ in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2008, 17:58
  4. Quitting before the QApplication::exec()
    By jsmax in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2008, 20:19
  5. readLine problem
    By gQt in forum Qt Programming
    Replies: 10
    Last Post: 29th February 2008, 09:35

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.