Results 1 to 5 of 5

Thread: Qt Console Application: while loop blocks event loop

  1. #1
    Join Date
    May 2011
    Posts
    8
    Qt products
    Qt4

    Default Qt Console Application: while loop blocks event loop

    Hello,

    Im making a console application where Im getting user input with std::getline() (in a while loop). Im also using QextSerialPort (inherits QIODevice) and readyRead() signal. Thus I need an event loop so I'm using QCoreApplication and calling a.exec().

    The problem is getline() blocks event loop and, while it's blocked, I can't receive anything from serial port. I tried to use QCoreApplication:rocessEvents() in the while(1){getline()} loop, but found that's not a good solution. Then, I tried to call QCoreApplication:rocessEvents() in a QThread but it didn't work.

    Here's the code Im using:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. Base *base = new Base();
    6. Commander *commander = new Commander(base);
    7.  
    8. QObject::connect(commander, SIGNAL(end()), &a, SLOT(quit()));
    9. QTimer::singleShot(0, commander, SLOT(run()));
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Commander::askToConnect()
    2. {
    3. if(base->connect() == false){
    4. cout << "Failed to open port." << endl;
    5. }
    6. }
    7.  
    8. void Commander::run{
    9. string inputline;
    10.  
    11. askToConnect();
    12.  
    13. while(1){
    14. QCoreApplication::processEvents(); // ???
    15.  
    16. cout << endl << "> ";
    17. getline(cin, inputline);
    18.  
    19. // ...
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    Even if I have while(1); the event loop is blocked and I can't read data from serial port.

    So, how can I get user input without blocking event loop?

    Thanks!

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qt Console Application: while loop blocks event loop

    This is drawback of using standard console. There is no easey solution.

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt Console Application: while loop blocks event loop

    Blocking input routines such as getline() block the thread which runs them. I see two options:
    1. Move all the input on stdin to a dedicated thread that will send each read line to the main thread (e.g. using signals and slots). Tip: if you need to exit the application, I guess you can close stdin to make the thread return from the blocking call.
    2. Use non-blocking input on stdin. This will be trickier and highly platform-dependant. Libraries such as ncurses might help you here.

    I would recommend using solution 1.

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

    Default Re: Qt Console Application: while loop blocks event loop

    Use a QSocketNotifier on stdin and query input only when there is something to read.
    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.


  5. #5
    Join Date
    May 2011
    Posts
    8
    Qt products
    Qt4

    Default Re: Qt Console Application: while loop blocks event loop

    Quote Originally Posted by yeye_olive View Post
    Blocking input routines such as getline() block the thread which runs them. I see two options:
    1. Move all the input on stdin to a dedicated thread that will send each read line to the main thread (e.g. using signals and slots). Tip: if you need to exit the application, I guess you can close stdin to make the thread return from the blocking call.
    2. Use non-blocking input on stdin. This will be trickier and highly platform-dependant. Libraries such as ncurses might help you here.

    I would recommend using solution 1.
    yeye_olive, I sticked with the first option. getline() is now in a dedicated thread and I send the "inputline" through a signal. It's working!

    Thank you all for your help!

Similar Threads

  1. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  2. Application won't quit until event loop quits
    By caelestis in forum Qt Programming
    Replies: 6
    Last Post: 11th February 2010, 07:21
  3. Replies: 10
    Last Post: 15th January 2010, 14:35
  4. Qt plug-in for GLib event loop based application
    By profoX in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2008, 14:27
  5. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.