Results 1 to 4 of 4

Thread: CLI and QCoreApplication - where to start?

  1. #1
    Join Date
    Jan 2006
    Location
    germany
    Posts
    75
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default CLI and QCoreApplication - where to start?

    I want to write a little program with a CLI. How do I actually implement this?
    The Tutorials/examples were not really about CLIs and searching didnt help me yet...

    main.cpp:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include "myapp.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7. MyApp *realApp = new MyApp(argc,argv);
    8. return realApp->run();
    9. }
    To copy to clipboard, switch view to plain text mode 
    doesnt look good, because no eventloop is created...
    should i instead let myapp inherit QCoreApplication and just reimplement myapp::exec() to do what i want it to?
    Or what is the "right" way to do it?

    Thank you very much for your help!
    Quote Originally Posted by Bertolt Brecht
    What is the robbing of a bank compared to the founding of a bank?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: CLI and QCoreApplication - where to start?

    to create an eventloop you need to use QCoreApplication::exec() just like you do with QApplication::exec() in GUI apps.

    Insertt app.exec() after myApp->run() and you have your eventloop.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CLI and QCoreApplication - where to start?

    Quote Originally Posted by soul_rebel View Post
    How do I actually implement this?
    If you want to use the event loop, your CLI application must be event-driven.

    Qt Code:
    1. int main( int argc, char **argv )
    2. {
    3. QCoreApplication app( argc, argv );
    4. MyApp realApp( argc, argv );
    5.  
    6. // schedule a call to "run"
    7. QMetaObject::invokeMethod( & realApp, "run", Qt::QueuedConnection );
    8. // if you prefer a shorter, but more cryptic way, use:
    9. // QTimer::singleShot( 0, & realApp, SLOT( run() ) );
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    (of course MyApp::run() has to be a slot).

  4. The following user says thank you to jacek for this useful post:

    soul_rebel (21st October 2007)

  5. #4
    Join Date
    Jan 2006
    Location
    germany
    Posts
    75
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: CLI and QCoreApplication - where to start?

    Thanks for your answers! I actually implemented it Friday and used a SingleShot-Timer like you propose.
    That works great!

    Also thanks for showing me that QMetaObject::invokeMethod exists
    That looks very useful for Threaded Applications with multiple eventLoops.
    Quote Originally Posted by Bertolt Brecht
    What is the robbing of a bank compared to the founding of a bank?

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.