1

Sorry if this sounds a bit newish. I am writing a cross-platform application to learn C++, which, if it turns out to work well, could help people. I would like it to be able to be used in two ways: by the designed GUI (may be using QT), or by command line for expert users who want to go beyond.

I would like to know how the communication between GUI application and the core should be made, since this is going to be open source and I want the code to be nice and clean.

Is there any commonly accepted way to do this?

mesafria
  • 311
  • 2
  • 10
  • I make a lot of my GUI applications accept command line parameters. I process these in my QApplcication class. If the GUI does not need to be shown I do not call it or use app.exec(). Although in other applications I use the same base libraries but do not include the GUI. – drescherjm Aug 21 '15 at 15:16
  • To get answer on your question you must read some books. For example: http://www.stevemcconnell.com/books.htm http://erdani.com/index.php/books/ – Dmitry Sazonov Aug 21 '15 at 15:23

1 Answers1

4

There are a few approaches you could follow.

  1. First write the CLI, then write the GUI as a standalone program that spawns the CLI version for all processing (via the system(), popen(), fork(), g_spawn(), CreateProcess() or similar calls). This may be a little more tedious than writing a library, but works well if you have already written the CLI, or in cases such as batch processing where the GUI is just a fancy form where you choose the parameters.

  2. Split the application not into two, but three parts: library, GUI and CLI. The library would contain all the shared logic between GUI and CLI, such as handling input formats, editing operations, and so on. The GUI part would implement the graphical interface using the functions from the library, and the CLI would implement the command-line interface using the same library.

  3. Make the GUI application accept command-line parameters, and avoid initializing the widget library if the passed parameters indicate that it is invoked in CLI mode. This is OK for a primarily graphical application such as a bitmap editor or a programming IDE that needs to expose some operations for scripting. Note that some extra hacking is required to make this work on Windows, see this answer. Also note that this will make it harder to run the app on servers that don't have a windowing system, since it will still have GUI libraries linked in.

Community
  • 1
  • 1
Krzysztof Kosiński
  • 4,225
  • 2
  • 18
  • 23
  • 1
    Thank you very much, this really helped. By the way, I had a doubt, you had an answer, you helped me, and future viewers. Really don't understand the downvotes. – mesafria Aug 21 '15 at 16:09