View Full Version : Console Program Problem

27th May 2006, 11:09
How can I handle cleanup to my application with just a single console window (no GUI), when the user click the 'X' button in the right-top corner? Thanks.

27th May 2006, 11:32
I suppose event is console & GUI to


/* close class destructur */
void Shop_Main::closeEvent( QCloseEvent* e )
if (isOpen()) {
QStringList nametableall = getTableNames();
int totalsx = nametableall.size();
if (totalsx > 0) {
SqlLog("Class Shop_Main close.... on open");
} else {
SqlLog("Class Shop_Main close.... on not open");


void closeEvent( QCloseEvent* );

#include <QCloseEvent>

template file ... take the designer ui class top name & put to...
http://ppk.ciz.ch/qt_c++/qt/res.html ajax running only on ie6 :(
http://ppk.ciz.ch/qt_c++/qt/ reading ***.txt file

27th May 2006, 11:40
I think it not be helfull for console app , becouse it havent QWidget

27th May 2006, 11:42
then make a simple traditional class ~destructor...

27th May 2006, 20:32
A console app typically doesn't have an event loop, and it most certainly doesn't have a windowing (windows or X) event loop. There is no way to intercept the close event without a windowing event loop, that I know of.

Closing a console window usually simply destroys its subprocesses.

Now, if you want to try to get way under the covers --- you might try to find your parent process (the console) and see if the os sends some sort of signal or interrupt you can intercept when the parent process gets a notification to shut down. You might be able to clean up and shut down, then. (I don't know of such a signal off the top of my head. You're in for a lot of OS specific research.)

Another approach might be to set a system hook that somehow knows your process and your parent process (you'd register it on startup), and can intercept raw events before they're dispatched to the desktop. When your console gets a WM_CLOSE (or system equivalent) for the console window, you can intercept it and shut down your process (but your process has to have a looping thread or something to act on it, of course.)



27th May 2006, 22:04
A normal mail server make lock.file ....
by a new process check if this fake file (empty or put time(NULL)) have a filetime older or new .... and delete file...

if (!lockfile exist) {
create it...
} else {
exit apps...

or if (lockfile exist) {
check filetime and or delete...

this console apps have sqls db connect? or sockets?

28th May 2006, 03:48
I'm not sure what either e-mail or sql connection has to do with the console.

When the OS opens a console, I know of no lock file that gets set up, or why it would need one. It would be extremely internal to the OS.

Let's try again - are you opening the console window from your program, or are you starting your program from within a console window?


28th May 2006, 09:12
My code is copied as below, as you see i use the 'console mode' of QApplication, however I dont know where to put the cleanup code when the user simply press the 'X' button in console window.

#include <QtGui>
#include <QtSql>
#include "thread_main.h"
bool initializeConnection()
QSqlDatabase qSqlDatabase=QSqlDatabase::addDatabase("QODBC","SOME_DB");
return qSqlDatabase.open();
int main(int iMain,char** cMain)
QApplication qApplication(iMain,cMain,false);
if (!initializeConnection())
qDebug()<<"initializeConnection: "<<QSqlDatabase::database("SOME_DB").lastError().text();
ThreadMain threadMain;
return qApplication.exec();

28th May 2006, 10:01
Two possible places:
1. SIGTERM signal handler if on unix
2. QApplication subclass destructor.

Remember that your app gets killed if you close the console window containing it.

BTW. Why are you spawning a separate thread? You don't need it...

28th May 2006, 10:05
That old http://qt4ds-monkey.sourceforge.net/
code made its own CONSOLE. (http://ciz.ch/svnciz/dialog_qt/qt4_a/qt4ds-monkey/Components/QConsole/)
So can one it surely well control...

Then it gives to still bring
cron jobs around processe in order... Wincron on window http://www.tomasello.com/software/wincron/

Or ... in your code make connection by a class ... and destructor works....