GUI app print to console
-
wrote on 9 Jun 2018, 21:50 last edited by
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. -
wrote on 8 Jul 2018, 16:42 last edited by
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"
?
-
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. -
wrote on 8 Jul 2018, 16:52 last edited by
Thank you and does have Qt access to some of these API calls? Or it must be done in pure C++?
-
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 -
wrote on 8 Jul 2018, 17:12 last edited by
Thank you :) I consider Qt as framework for C++ :) By pure C++ I meant code without Qt classes :)
-
Thank you :) I consider Qt as framework for C++ :) By pure C++ I meant code without Qt classes :)
Lifetime Qt Championwrote on 8 Jul 2018, 17:17 last edited by mrjj 7 Aug 2018, 17:17@t0msk
hi
ok, well it would be ok fine to do in a Qt app.
but its not crossplatform. -
@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
@t0msk
hi
Yes, that's the normal way to have different code for
each platform since same calls wont work.
So if you wrap it up in a nice class, and add code for each platform
it will be crossplatform :) -
Hi,
Qt already provides macros for the OS it supports. See here.