Results 1 to 10 of 10

Thread: Console Program Problem

  1. #1
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    3
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Console Program Problem

    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.

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Console Program Problem

    I suppose event is console & GUI to


    ****.cpp

    Qt Code:
    1. /* close class destructur */
    2. void Shop_Main::closeEvent( QCloseEvent* e )
    3. {
    4. qt_unlink(LOCK_FILE_APPLICATION);
    5. if (isOpen()) {
    6. QStringList nametableall = getTableNames();
    7. int totalsx = nametableall.size();
    8. if (totalsx > 0) {
    9. dumptofilesql(LAST_SQL_SESSION);
    10. }
    11. SqlLog("Class Shop_Main close.... on open");
    12. } else {
    13. SqlLog("Class Shop_Main close.... on not open");
    14. }
    15. DownDirRemove(DB_IMPORT_CACHEDIR);
    16. e->accept();
    17. }
    To copy to clipboard, switch view to plain text mode 


    ****.h

    Qt Code:
    1. protected:
    2. void closeEvent( QCloseEvent* );
    3. //
    To copy to clipboard, switch view to plain text mode 

    #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
    Last edited by patrik08; 27th May 2006 at 11:35.

  3. #3
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Console Program Problem

    I think it not be helfull for console app , becouse it havent QWidget
    a life without programming is like an empty bottle

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Console Program Problem

    then make a simple traditional class ~destructor...

  5. #5
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Console Program Problem

    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.)

    Non-trivial.

    rickb

  6. #6
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Console Program Problem

    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?

  7. #7
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Console Program Problem

    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?

    rickb

  8. #8
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    3
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Console Program Problem

    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.

    Qt Code:
    1. //------------------------------------------------------------------------------
    2. #include <QtGui>
    3. #include <QtSql>
    4. //------------------------------------------------------------------------------
    5. #include "thread_main.h"
    6. //------------------------------------------------------------------------------
    7. bool initializeConnection()
    8. {
    9. QSqlDatabase qSqlDatabase=QSqlDatabase::addDatabase("QODBC","SOME_DB");
    10. qSqlDatabase.setDatabaseName("some_db");
    11. return qSqlDatabase.open();
    12. }
    13. //------------------------------------------------------------------------------
    14. int main(int iMain,char** cMain)
    15. {
    16. QApplication qApplication(iMain,cMain,false);
    17. if (!initializeConnection())
    18. {
    19. qDebug()<<"initializeConnection: "<<QSqlDatabase::database("SOME_DB").lastError().text();
    20. }
    21. ThreadMain threadMain;
    22. threadMain.start();
    23. return qApplication.exec();
    24. }
    25. //------------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 28th May 2006 at 10:01.

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

    Default Re: Console Program Problem

    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...

  10. #10
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Console Program Problem

    That old http://qt4ds-monkey.sourceforge.net/
    code made its own CONSOLE. (http://ciz.ch/svnciz/dialog_qt/qt4_a...ents/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....

Similar Threads

  1. No output to console
    By Morea in forum Newbie
    Replies: 6
    Last Post: 1st November 2007, 23:51
  2. Enter key causing program to exit
    By welby in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 17:11
  3. Replies: 16
    Last Post: 7th March 2006, 16:57
  4. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 12:35
  5. Problem with Signal and Slots
    By Kapil in forum Installation and Deployment
    Replies: 2
    Last Post: 10th February 2006, 09:51

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.