Integrating C++ Software into Qt Desktop Application
-
I've developed a C++ software application that currently runs from the terminal and requires multiple parameters, such as specifying file paths, to execute various tasks. Now, I'm looking to transition this command-line tool into a desktop application using Qt, with a graphical user interface (GUI) to streamline user interaction.
I'm seeking guidance on how to integrate my existing C++ software, which relies on command-line parameters, into a Qt project to develop a desktop app with a user-friendly interface. Specifically, I'm interested in:
1-How should I structure my Qt project to accommodate the integration of my existing C++ codebase and its reliance on command-line parameters?
2-Are there any recommended design patterns or strategies for managing user input and executing tasks based on the provided parameters within a Qt application?
I appreciate any insights, examples, or resources that can help me successfully integrate my C++ software with command-line parameters into a Qt desktop application. Thank you for your assistance!
-
Hi and welcome to devnet,
There are mainly two approaches:
- Build your UI and use QProcess to call your command line tool keeping them independent
- Have the business logic of your command line tool in a library so your can reuse it to build both your CLI and GUI applications.
-
If you want an integrated application (not the variant with QProcess that @SGaist suggested), the first step would be to separate your command line application: parse the command line parameters into variables and the actual functionality should then be refactored to a separate function, that takes these variables as function arguments. This ensures, that the same function can be called from your GUI application. Inside the GUI application you would then have input widgets for each variable and a button labeled "Run" (or similar). When "Run" is clicked, you collect all the different inputs and call your refactored function. If this takes longer to run, you should put it into a separate thread (and then the other solution "use a QProcess" is not too different to implement, though for the perfect solution threads should be used, but QProcess will be easier). Maybe for threading a simple QtConcurrent::run() will suffice.
You will have some trouble if there is important console output (writing to stdout/stderr) of your existing application. How do you want to interact/signal errors? For a good solution this would require deeper changes into your existing application. With the QProcess approach the only thing you can do is to show the output to the user. Otherwise, you could use callback function for output. In the case of your command line application this can still write to the console by default, and for your GUI application you register a callback that shows a QMessageBox. Maybe the callback can even return a value (e.g. from the message box) to continue or abort.