
Originally Posted by
ulix
Hello I'm trying to implement a signal handler mechanism into my Qt Console application to quit it when a unix signal is catched
So I used the standard library c function
uno online signal in signal.h registering a handler for SIGTERM signal.
Then I start doing some stuff and in particular I'm reading the stdin using a QTextStream(stdin).readline() method.
The point is that when I'm waiting for the input on the readline the application does not catch the SIGTERM signal
Can someone explain me why?
thank you in adavance
As I know, QApplication does not handle SIGTERM signal and exit gracefully by default. You may need to manually handle the signal by connecting it to a slot that calls QCoreApplication::quit() to exit the application. Here’s an example of how you can handle the SIGTERM signal in your application:
#include <QCoreApplication>
#include <signal.h>
void signalHandler(int signalNumber)
{
if (signalNumber == SIGTERM) {
}
}
int main(int argc, char *argv[])
{
// Register the signal handler for SIGTERM
signal(SIGTERM, signalHandler);
// Do some stuff here
return app.exec();
}
#include <QCoreApplication>
#include <signal.h>
void signalHandler(int signalNumber)
{
if (signalNumber == SIGTERM) {
QCoreApplication::quit();
}
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
// Register the signal handler for SIGTERM
signal(SIGTERM, signalHandler);
// Do some stuff here
QTextStream(stdin).readLine();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
This code registers a signal handler for SIGTERM using the signal() function from signal.h. When the SIGTERM signal is caught, the signalHandler() function is called, which calls QCoreApplication::quit() to exit the application gracefully. I hope this helps! Let me know if you have any other questions.
Bookmarks