GUI app print to console
-
Hello,
I am creating app with GUI (widgets) for Linux. If you run my app it shows GUI (widget), but I want to make my app to be available for console only Linux too (without desktop environment).
So is possible that if someone runs my app through console it will not shows GUI (because you can't) but instead of that I will outputs some text into console?
-
Hi
yes, its possible.
Doc has sample.
http://doc.qt.io/qt-5/qapplication.html#details
The trick is to use QCoreApplication instead of QApplication and
a parameter telling when to run non gui.But for this to work as intended, you must design your app
so any data it needs or function do not
live in mainwindow or other widgets.
Then your app can work in both modes. -
Thank you very much, sorry for late reply, just one more question this is only one way?
Or is possible that program will detect if it is run from command line or just by arguments like:
"-no-gui"
?
@t0msk
Well it uses the parameter to know, so it needs some flag in some form or another.
I have not seen other methods, but some could exists involving navtive api calls
to find out if run from a shell or via other means of activation. -
Thank you and does have Qt access to some of these API calls? Or it must be done in pure C++?
@t0msk
Hi
Qt is pure c++. :)
But there would not be a Qt class for it.
You would use native windows / linux apis
something like shown here
https://stackoverflow.com/questions/9009333/how-to-check-if-the-program-is-run-from-a-console -
Thank you :) I consider Qt as framework for C++ :) By pure C++ I meant code without Qt classes :)
-
@mrjj I think it could be cross platform, if I added support for Linux, Windows and Mac like:
#ifdef __linux__ //linux code goes here #elif _WIN32 //windows code goes here #else #endif