Results 1 to 2 of 2

Thread: QtCoreApplication console app

  1. #1
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QtCoreApplication console app

    I am trying to write a console-based QtCore application but am unable to figure out how to do so.

    According to the documentation, QCoreApplication should be used for command-line apps.

    I have the following code:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication app(argc, argv);
    4.  
    5. CLIClient *client = new CLIClient;
    6. client.run();
    7.  
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    client.run() contains a while loop which never exits which drives the command-line interface. However, since app.exec() is never called I can't use QProcess and other QtCore components.

    How can I use the QCoreApplication event loop which is alluded to in the documentation?

  2. #2
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtCoreApplication console app

    You could start your loop with a slot. Something like this:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication app(argc, argv);
    4.  
    5. CLIClient *client = new CLIClient;
    6. QTimer::singleShot(0, client, SLOT(run()));
    7.  
    8.  
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    So your run() function will be called as soon as the Qt event loop starts. Of course CLIClient must be a QObject (or you can wrap it with a QObject).
    Anyway, if your main loop does not ever return until the end of the program you will still have some problems, because the Qt event loop will not be executed until then.

  3. The following user says thank you to bender86 for this useful post:

    titaniumdecoy (18th October 2009)

Similar Threads

  1. How to write qt4 gui up to the existing console
    By binaural in forum Qt Programming
    Replies: 4
    Last Post: 16th June 2009, 14:44
  2. convert console to windows app
    By Max Yaffe in forum Newbie
    Replies: 1
    Last Post: 13th June 2007, 16:38
  3. Console replacement
    By aegis in forum Qt Programming
    Replies: 14
    Last Post: 3rd April 2007, 01:38
  4. How to run a console program "silently"?
    By fullmetalcoder in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2006, 11:03

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.