Calling command prompt from Qt application without freezing? - c++

In my Qt GUI application, I am calling the command prompt through:
system("lots.exe & of.exe && commands.exe");
It opens up the command prompt (like I want it to), but freezes the Qt GUI application until I close the command prompt. Is there someway to prevent this? I saw that there is a QProcess class, but can't get it to bring up the command prompt.
Any help would be greatly appreciated!

QProcess is really the answer. If you want to use something like system() you'll have to either put the call in another thread or use popen or something simmilar for your platforms.
QProcess does have the setReadChannel which you could use to display your own console window to show the output.

You just need to put that system call in a separate thread.

If you do not need any of the output, the easiest way would be to use QProcess::startDetached().
http://doc.qt.io/archives/4.6/qprocess.html#startDetached
If you do need the output, QtConcurrent::run with a futurewatcher containing the output would be less overhead/work than deriving QThread.

Related

How to run a command in C++ without a CMD window showing up?

So, to be more specific, I want to make a C++ program (Windows) that can execute a command without anything being displayed to the user.
No CMD window, No Command Output.
I would show my previous attempts, but it's not really anything useful, I just used the system() function to execute a command. I need to do the same, but without a popup window.
system() uses CreateProcess() to run cmd.exe /C <command>. You have no control over the console window that may be created.
Use CreateProcess() directly instead, so you can specify the CREATE_NO_WINDOW flag, and/or set its input STARTUPINFO::wShowWindow field to SW_HIDE.

QProcess with CreateNoWindow

In C# there is an attribute which enables application to run 3rd party apps without showing the app window.
Is there any way to run a console application without showing the console window in QT without using Win32 CreateProcess function?
QProcess.start() will run the console application without showing its window, but you may also wish to have some control over it. Please see this example:
QProcess p;
p.setProcessChannelMode(QProcess::MergedChannels);
p.setStandardOutputFile("out.txt");
p.start("cmd.exe", QStringList()<<"/C"<<"ping"<<"127.0.0.1");
p.waitForStarted();
p.waitForFinished();
You can pass commands and parameters to the console using second argument in the start method (inside QStringList). It is also possible to redirect the output to some file, with setStandardOutputFile method.
If you need to show the window, use p.startDetached().

QProcess STDIN with show console

I want to create a process under QProcess with console show and acccess STDIN and STDOUT streams.
my code:
QProcess *p1 = new QProcess(this);
p1->start("cmd.exe");
if I want to show console I must use startDeatached() function, but by using this, I lost my STDIN/OUT access in my program.
if I want to have access these, I dont have my console show!!!???
help me tanx.
after more investigations, I did not find my exact answer. But I did a quick trick (kalak rashti) for this problem.
I used a QTextEdit custom class to simulate some thing behaves like console. frome this link:
https://code.google.com/p/qterminalwidget/source/browse/trunk/
with some changes for my purpose.

Win32 application with console output and without new window

I'd like to create a tool that can either act as command line (display some console output based on input parameters), or display a window, based on input parameters.
I'm using MSV2012 with C++, and it seems you have to 'choose' between console and window app.
I know the net is filled with samples which use AllocConsole() and redirect std::out, but it doesn't make it feel like a command line application : calling the exe from the windows console will open a new window with the console output...
Is there a way to have it use the current console window instead of allocating a new one?
If not possible, I'll make 2 applications instead, but that's a pity..
Someone else may have a more authoritative answer, but I don't believe it's supported.
The usual workaround is to create a Windows app, but have a command-line wrapper that launches it from the CLI (and provides a channel for communicating with the original console).
It's not technically supported but I found a good solution by getting a snapshot for the current process, finding the parent process, attaching to it's console if it's a console app or creating one with AllocConsole, redirecting the output, getting the thread of the parent process if it's cmd.exe and suspending it, resuming it just before I exit my app

Open console inside gui application

I'm trying to create a program in C++, which open with him, within the form, the CMD, so that I can run a program within the CMD that is inside the program. As an example, in this photo:
I found some examples, but could not implement them in code, so, I ask your help... To be more precise, I want to create a function in a dll, so I can call this CMD, starting from any language
basically an embedding a Console
I see two methods:
Write your own console widget and pass all entered command to the std::system command
Do what is described in How to open a console window in a Win32 application