Results 1 to 5 of 5

Thread: Issue with Unix signals and QTestStream readline

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Jan 2025
    Location
    California
    Posts
    2

    Default Re: Issue with Unix signals and QTestStream readline

    Hello

    The issue arises because QTextStream(stdin).readLine() is a blocking call, and signals like SIGTERM are not handled immediately during such operations. The signal handler will execute only after the blocking call completes.

    Solution:
    Use non-blocking I/O with QSocketNotifier to read from stdin asynchronously, allowing signals to be handled promptly:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSocketNotifier>
    3. #include <QTextStream>
    4. #include <signal.h>
    5.  
    6. QCoreApplication* app = nullptr;
    7.  
    8. void signalHandler(int) {
    9. if (app) app->quit();
    10. }
    11.  
    12. int main(int argc, char* argv[]) {
    13. QCoreApplication application(argc, argv);
    14. app = &application;
    15.  
    16. signal(SIGTERM, signalHandler); // Register signal handler
    17.  
    18. QSocketNotifier notifier(STDIN_FILENO, QSocketNotifier::Read);
    19. QObject::connect(&notifier, &QSocketNotifier::activated, [&]() {
    20. QTextStream input(stdin);
    21. QString line = input.readLine();
    22. if (!line.isEmpty()) qDebug() << "Input:" << line;
    23. });
    24.  
    25. return application.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    This ensures signal handling works even while waiting for input.
    Last edited by d_stranz; 10th January 2025 at 15:47. Reason: missing [code] tags

Similar Threads

  1. Replies: 6
    Last Post: 2nd May 2012, 09:13
  2. qThread and Unix Signals
    By peterjb31 in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2011, 11:24
  3. Performance issue when using signals
    By baluk in forum Newbie
    Replies: 2
    Last Post: 18th November 2010, 04:07
  4. Replies: 7
    Last Post: 29th May 2009, 08:58
  5. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18

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
  •  
Qt is a trademark of The Qt Company.