Results 1 to 13 of 13

Thread: Console application on the side

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Console application on the side

    Hello there,

    I developed a Qt application with gui and everything for the running and visualization of physics-related experiments. Sometimes I have to start a long running process that can take several days to try out many things in the background and in the end reports with the found results. I am looking for a way to launch this background process without a gui just by invoking a command in the shell. The process is in an Object that derives from QThread and can run on its own. As far as I see it, it is really just a question of building the project right. Does it makes sense to have two project configurations, where one would build a gui application and the other just a console application? If this is the right way, would I have two different .pro files then? Would I need two .cpp classes that have a main() function tailored to my needs?

    Thanks,
    Cruz

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,530
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Console application on the side

    Maybe a command line parameter that will say whether to run the GUI or not?

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

    Cruz (5th June 2021)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Console application on the side

    Yes, true, much easier that way. Thanks.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Console application on the side

    It's about as simple as not calling show() on the main window instance when the "run in the background" command line parameter is present. You could also simply make a copy of your executable, rename it, and examine the name of the executable at run time to decide how to run it up. QCoreApplication::applicationName()
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    Cruz (5th June 2021)

  7. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Console application on the side

    I gave this a go, but it does not work as expected yet. It does when I start my program locally, but when I try to start it remotely over ssh, I receive the error message:
    qt.qpa.screen: QXcbConnection: Could not connect to display
    Could not connect to any X display.
    I was able to find hints that I should forward my X session, but this does not come easy to me, because I have to first ssh into a proxy server and then from there I can ssh into a protected machine. I would like to avoid any X and windows and widgets altogether. My main function looks like this:

    Qt Code:
    1. #include "GuiSimulation.h"
    2. #include "Experimenter.h"
    3.  
    4. #include <QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. GuiSimulation w; // The gui.
    10. Experimenter e; // The background thread.
    11.  
    12. if (argc > 1)
    13. {
    14. e.start();
    15. }
    16. else
    17. {
    18. w.show();
    19. }
    20.  
    21. return a.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 
    Even if I minimize this to

    Qt Code:
    1. #include <QApplication>
    2. int main(int argc, char *argv[])
    3. {
    4. QApplication a(argc, argv);
    5. return a.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 
    I receive the error message above. Any help is much appreciated.

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,530
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Console application on the side

    You should use QCoreApplication in command line mode.

  9. The following user says thank you to Lesiok for this useful post:

    Cruz (7th June 2021)

  10. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Console application on the side

    Indeed! The final result:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. if (argc > 1) // Command line mode.
    4. {
    5. QCoreApplication a(argc, argv);
    6. Experimenter e;
    7. e.start();
    8. return a.exec();
    9. }
    10. else // GUI mode
    11. {
    12. QApplication a(argc, argv);
    13. GuiSimulation w;
    14. w.show();
    15. return a.exec();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

  11. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Console application on the side

    One more question though. The event loop I started with a.exec() keeps running forever and eventually I have to kill it by hand. Normally, I would connect the finished() signal of QThread to a closed() or exit() slot or something, but here in the main function I can't use connect() right? How do I go about this?

  12. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Console application on the side

    but here in the main function I can't use connect() right?
    Why not? There is a static version of QObject::connect(), and QCoreApplication is derived from QObject, so you could also use a.connect().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  13. The following user says thank you to d_stranz for this useful post:

    Cruz (9th June 2021)

  14. #10
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Console application on the side

    Yes! This works like a charm. Thanks.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. if (argc > 1) // Command line mode.
    4. {
    5. QCoreApplication a(argc, argv);
    6. Experimenter e;
    7. e.start();
    8. a.connect(&e, SIGNAL(finished()), &a, SLOT(quit()));
    9. return a.exec();
    10. }
    11. else // GUI mode
    12. {
    13. QApplication a(argc, argv);
    14. GuiSimulation w;
    15. w.show();
    16. return a.exec();
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

  15. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Console application on the side

    I think I would call connect() before starting the Experimenter thread. If the thread finishes before the connection is made, you'll get a hung executable.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  16. The following user says thank you to d_stranz for this useful post:

    Cruz (10th June 2021)

  17. #12
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Console application on the side

    Yes, you are right. This thing here takes several days to run so it shouldn't be an issue, but why would we stop short of perfection...

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. if (argc > 1) // Command line mode.
    4. {
    5. QCoreApplication a(argc, argv);
    6. Experimenter e;
    7. a.connect(&e, SIGNAL(finished()), &a, SLOT(quit()));
    8. e.start();
    9. return a.exec();
    10. }
    11. else // GUI mode
    12. {
    13. QApplication a(argc, argv);
    14. GuiSimulation w;
    15. w.show();
    16. return a.exec();
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

  18. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Console application on the side

    but why would we stop short of perfection...
    Exactly. Someone else might read this thread and copy your next to last version, then wonder why it didn't work because their process is very quick.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 5
    Last Post: 12th January 2020, 16:55
  2. Replies: 2
    Last Post: 30th January 2015, 09:29
  3. Replies: 8
    Last Post: 23rd August 2012, 19:28
  4. Replies: 2
    Last Post: 21st November 2010, 18:03
  5. Replies: 5
    Last Post: 15th February 2008, 02:54

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.