I am a beginner is C++. I am trying to find, is it possible to run my program in both in QT window and Linux based. When the user logins into my system, the user can select GUI or terminal mode to run the system.
Thus, I would like to know is it possible to do it. If possible how can I proceed on? What command should I use to switch from a QT window to a terminal?
Do I need to create a separate set of project for both individually or using the same set of classes?
All Linux programs (unless explicitly disabled) print out text to a terminal. If you run the program in a graphical environment you will probably not run it from a console, therefore you won't see the output, but it will be still there.
If you want your program to be usable from a console, just test whether you could create the main window and if not, fallback to simple text output.
Note that the binary will still require the X server and Qt libraries to be installed.
You can construct your application with or without the GUI enabled through the QApplication constructor. Consult the example in Qt's documentation:
http://qt-project.org/doc/qt-4.8/qapplication.html#QApplication-2
Though, it should be noted that everything in Let_Me_Be's response is correct. In fact, the Qt example does exactly what he's suggesting. Please take the time to understand his answer before you plunge into coding.
Related
I'm looking to create a cross-platform readline/linenoise type command line interface for an application in Qt. I'd rather not reinvent the wheel if possible. The terminal in which the user is operating should be able to receive commands via a command prompt. This will be done in Qt, thus my question is: is there a Qt-esque way of doing this via signals and slots such that when a user enters a line into the terminal a slot can be called?
I understand that this can be fairly simply done using a QThread and running a blocking process to signal on a line being read. This question is specifically geared towards using built-in Qt functionality.
While attempting to discern a viable answer to this, I realized that my question was improperly phrased. I was looking for what could be termed a Terminal Emulator. There are several existing solutions to this, including QTermWidget. However, it appears that there does not yet exist a non-UI based terminal emulator for Qt. That is, in order to use these one must have an active UI instance of Qt, not merely employing a Qt Command Line application.
Going the route of using a simple Qt Command Line, it is not as simple as connecting something like a QTextStream and observing the output of the command line. As with most command line programs, native, low-level processing must occur in order to assess things like the arrow keys. Unfortunately QKeyPressEvent and the like require the UI to be running.
I am programming in C++ using Qt-4 as framework, given a directory (i.e. /usr/bin) i would like to know whether the program is a GUI or console application.
I came across some information on how to do this windows and also about the inexistence of a similar (to Win) identification on Linux (within elf). Is there an alternative way to perform this on Linux?
I thought on:
spawning each entry with QProcess and monitor it's lifetime assuming a console application would require some input and as i did not provide any, it would print to stoud an help message and exit. If it's alive after x-seconds, i would kill the process and tag it as containing a gui. It is an horrible approach and error-prone;
run ldd and parse the output for any libX or else. It seems a better approach although firefox, for example, would fail to be tagged as having gui.
EDIT0:
This project is an app launcher
EDIT1:
Once i have the list of programs categorized, i could launch a terminal emulator whenever the user chooses a non-graphical application
FINAL CONCLUSION:
After people answers and extra search, it really is not possible to reliably discern between console and gui applications. My best bet is to make several considerations like search for .desktop files, make a few assumptions like tools listed in utilities-only places like /bin, /sbin and /usr/sbin and so on.
Perhaps parse the output of ldd for each entry found.
Thanks.
If you're writing (yet another) app launcher, please follow what the people who have created all of the other app launchers did and use the .desktop files. That specification already defines the Terminal option which is exactly what you're looking for.
A program could try to start as a GUI program, and else switch to console.
(And actually some programs do exhibit such behavior, e.g. emacs). It could even do that at random, or because of some specific configuration...
For example, assuming that vi is a console program and emacs a graphical one, the following simple program may randomly be GUI or console:
#include <unistd.h>
int main(int argc, char**argv) {
if (getpid()%2 == 0)
{ argv[0]="vi"; execv("/usr/bin/vi", argv); }
else
{ argv[0]="emacs"; execv("/usr/bin/emacs", argv); }
return EXIT_FAILURE;
}
The simplest (but not foolproof) way of doing that is testing if getenv("DISPLAY") returns NULL. A more elaborate way would be to call XOpenDisplay which returns NULL on failure (and several X11 toolkits do that).
So, your question does not have a precise answer, and does not really make sense.
You could use ldd .... and add manually exceptions like firefox.
My application is a GUI app that has helpful (though optional) information through the terminal (via cout).
In Windows I either have a console appear (by compiling as a console app, or allocating it dynamically) or I don't.
My intention is to make use of the console IF it is being run from the console, but ignore the console completely if it was not. (Essentially what happens in Linux and OS X).
I do not wish to redirect to a file (and in the case of using cin, this is not a viable solution anyway).
Is there a way to attach a GUI app in Windows to the console it is run from, if and only if it is run from a console?
and in the case of using cin, this is not a viable solution anyway
This is the killer detail in your question. It is simple on paper, just first call AttachConsole(ATTACH_PARENT_PROCESS) to try to attach to an existing console. That will fail when your program got started from a GUI program like Explorer or a desktop shortcut. So if it returns FALSE then call AllocConsole() to create your own console.
Using cin is a problem however. The command processor pays attention to your EXE and checks if it is console mode app or a GUI app. It will detect a GUI app in your case and then doesn't wait for the process to complete. It displays the prompt again and waits for input. You will then also wait for input but you'll lose, the command processor got there first. Your output is also intermingled with the command prompt, the easy problem to solve.
There's a simple workaround for that, your user should start your program with start /wait yourapp to tell the command processor to wait for the process to complete. Problem is: nobody ever uses that. And the user will not realize what happens when they type input, intending it to go into your program but it is actually interpreted by the command processor. Producing a mystifying error message or formatting the hard drive.
Only two good ways to solve this unsolvable problem. Either build your program as a console mode app and call FreeConsole() when you find out you want to display a GUI. Or always call AllocConsole(). These are not great alternatives. The first approach is the one used by the Java JVM on Windows. One of the oldest bugs filed against the JVM and driving Java programmers completely batty from the flashing console window.
The third alternative is the only decent one, and the one you don't want, create another EXE that will always use the console. Like Java does, javaw.exe vs java.exe.
A trick is possible, you can rename that file from "yourapp2.exe" to "yourapp.com". It will be picked first when the user types "yourapp" at the command line prompt, a desktop shortcut can still point to "yourapp.exe". Visual Studio uses this trick, devenv.com vs devenv.exe.
You can check CONSOLE_SCREEN_BUFFER_INFO (via GetConsoleScreenBufferInfo) on startup to determine if you've been run from within an existing console. If the buffer's position is 0,0, you were run from outside of the console. For details, see this Microsoft Knowledgebase Article which describes the process.
In order for this to work, you need to compile your application as a console application (using /SUBSYSTEM:CONSOLE), and then detach yourself from the console if the application started a new console (buffer at 0,0). This will cause the program to properly "attach" to the calling console when launched from a command line.
As others have pointed out you have to create a console app and a window app. So, you'd end up with console.exe and app.exe. To make it less obvious at the command-line, you can take advantage of the PATHEXT trick like devenv does. cmd.exe treats a file as a command if its extension is in the PATHEXT environment variable. COM is there by default so you could rename console.exe as app.com, allowing the command app to start the console app attached to the current console.
Note: Of course, a console app can show a GUI if desired.
The difference in the build between app.com and app.exe depends on your build system but it could just be the one attribute that sets the output type. With msbuild (for .vcxproj files), it's just a matter of another build configuration.
you can create an application in console that get a line using argc and prints it;
////
int main(int argc, char *argv[])
{
//here print argv....using cout or printf
}
save the file as console.exe in the folder of your app.
now in your app if you want to see any line in console you can call the command
system("console.exe this is the line i want to print and see in console");
Is it possible to use GTK without a valid X display?
I want to use GTK with Webkit, but for now I'm only interested in using the DOM functions to experiment, so I have no need to actually display anything. It's very convenient for me to do this over an SSH connection on a server that has no display.
But, without a DISPLAY environment variable, gtk_init() simply fails with the message:
Gtk-WARNING **: cannot open display:
Is there anyway to use GTK without a display?
There is an X server called Xvfb which provides a valid DISPLAY and sends the output to a file instead of to graphics hardware. You can run that on the same machine that's running your gtk client, and gtk will be able to do whatever it wants to do without using any of your network bandwidth. As a bonus, you can read Xvfb's output buffer from the file, allowing you to get a screenshot of what would be on the screen.
While it's not an direct answer to your question, I think what you are looking for is a “headless” web browser. There's one based on WebKit, called PhantomJS. It doesn't require any GUI stack, and you can freely experiment with DOM there.
Gtk+ 3 (well, GDK) has support for multiple backends now, but still requires X11, Wayland, or Broadway (experimental HTML5 backend) to run.
DISPLAY doesn't necessarily need actual hardware; you can run a fake X server such as Xvfb or Xvnc.
I'm glad I read the docs deeper and didn't just believe this answer.
gtk_init_check()
This function does the same work as gtk_init() with only a single change: It does not terminate the program if the windowing system can’t be initialized. Instead it returns FALSE on failure.
Is it possible to embed system command line right inside your application window?
I want to be able to have the command line take some space on the bottom of my C++ application, and my application to take the above space. Ideally it should work on Mac, Linux and Windows, but for starters Windows is the primary goal as I am developing on it.
Speaking for Windows, the Windows console window is "special" in that it gets special treatment, different than any other window on the system. You will not succeed in manipulating it for your own use. For a good example, try using Spy++ on a console window.
Note that the window created with AllocConsole is not a command-line interpreter, it's just a console window.
You should implement your own console window, and as far as interpreting the commands I can think of a few options to explore depending on what you expect, behavior-wise and complexity-wise:
Delegate commands to a hidden cmd.exe
Interpret commands yourself
There are probably open-source solutions for command line interpreting.
In Windows, you can do that using some WinAPI functions like ShellExecute and CreateProcess (there are a few others that I don't remember). You get the command string from your GUI, pass it to one of these WinAPI functions, then send the output back to your GUI.
You want to do this on multiple platforms, so I would suggest making a generic module (class or namespace of functions, whatever fits you best) that allows using the OS terminal agnostic of the actual underling OS. Then. when you want to port your app to another OS, you just change the implementation of this module.
Note: Boost has (had?) a library under development that made running shell commands easier, Boost.Process. But it's currently on version 0.4 and hasn't been updated since October 4, 2010 (even though its status is still "on going").