There may be a way to do this natively with Qt -- I poked around the QKeySequence docs for a bit before giving up, but you can just use signal
. I don't have Qt/C++ setup on my machine at the moment, but I do have the Python bindings.
import sys, signal
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
sys.exit(app.exec_())
This works and will close the application when I do Ctrl-C. So I believe that your application could adapt this code and it would end up something like this:
#include <signal.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
... // parse command line options
if (no_gui) {
signal(SIGINT, SIG_DFL);
}
...
return app.exec();
}
Unfortunately, I cannot compile this so it will probably need a few fixes, but this should give you the general idea. By using the SIG_DFL
handler you are instructing your program to use the default action associated with Ctrl-C.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…