Launch telnet from Qt GUI application
-
I have a Qt Widgets application for Windows 11, and I would like to have a control on the front panel that launches a telnet window in a command prompt, which would run independently of the application until the user closes it. I am trying to find a way to do this with QProcess but nothing seems to work. The suggestions I've found online say to use QProcess to launch cmd.exe with the telnet command and parameters in the argument list, but it doesn't work. If I run my application from the Run menu in Qt Creator, I see some output in the Application Output tab, but it doesn't pop up a separate terminal. If I run it outside Qt Creator by double-clicking the executable, nothing happens. I tried using the C++ system command instead of QProcess -- this launches the telnet window but causes the GUI to be unresponsive until the window is closed. How can I make this work? It seems like there should be a way.
-
I have a Qt Widgets application for Windows 11, and I would like to have a control on the front panel that launches a telnet window in a command prompt, which would run independently of the application until the user closes it. I am trying to find a way to do this with QProcess but nothing seems to work. The suggestions I've found online say to use QProcess to launch cmd.exe with the telnet command and parameters in the argument list, but it doesn't work. If I run my application from the Run menu in Qt Creator, I see some output in the Application Output tab, but it doesn't pop up a separate terminal. If I run it outside Qt Creator by double-clicking the executable, nothing happens. I tried using the C++ system command instead of QProcess -- this launches the telnet window but causes the GUI to be unresponsive until the window is closed. How can I make this work? It seems like there should be a way.
@richferrara said in Launch telnet from Qt GUI application:
I am trying to find a way to do this with QProcess but nothing seems to work. The suggestions I've found online say to use QProcess to launch cmd.exe with the telnet command and parameters in the argument list, but it doesn't work.
I tried using the C++ system command instead of QProcess -- this launches the telnet window
Could you show precisely what you have tried (i.e. showing code and full command line) (a) with
QProcessand (b) withsystemcommand. May be able to help --- this is my area but I only run Qt under Linux so it may be guesswork! -
The first thing I tried was just launching telnet directly:
QStringList args = {"172.99.99.99"}; // using my target IP address
QProcess::startDetached("telnet.exe", {args});Then I read online that I needed to run the command prompt instead and include telnet in the arguments list:
QStringList args = {"/c", "telnet.exe", "172.99.99.99"}; // also tried "/k"; also tried no "/" argument
QProcess::startDetached("cmd.exe", args);I also tried putting in the complete path to cmd.exe and telnet.exe, then I tried creating a QProcess object and using qProcess.start() instead of the static startDetached. Nothing worked.
-
The first thing I tried was just launching telnet directly:
QStringList args = {"172.99.99.99"}; // using my target IP address
QProcess::startDetached("telnet.exe", {args});Then I read online that I needed to run the command prompt instead and include telnet in the arguments list:
QStringList args = {"/c", "telnet.exe", "172.99.99.99"}; // also tried "/k"; also tried no "/" argument
QProcess::startDetached("cmd.exe", args);I also tried putting in the complete path to cmd.exe and telnet.exe, then I tried creating a QProcess object and using qProcess.start() instead of the static startDetached. Nothing worked.
@richferrara
Start with, say:QProcess p; QStringList args = {"/k", "telnet.exe"}; p.start("cmd.exe", args); qDebug() << p.waitForStarted(3000); qDebug() << p.waitForFinished(3000); qDebug() << p.readAllStandardOutput() << p.readAllStandardError();Depending on what you get, try changing to:
QStringList args = {"/c", "dir /s C:\\"};First thing is to figure out that you're getting the command right. Then deal with needing a console to run the telnet in.
-
Running the telnet command, I see this in the Application Output tab:
true
false
"\r\nC:\[path to my executable]>" ""
QProcess: Destroyed while process ("cmd.exe") is still runningRunning the dir command, I see the output of dir start displaying in the tab.
I only see anything when I run my executable through Qt Creator's Run menu. If I run outside Qt Creator, I see no output.
-
Running the telnet command, I see this in the Application Output tab:
true
false
"\r\nC:\[path to my executable]>" ""
QProcess: Destroyed while process ("cmd.exe") is still runningRunning the dir command, I see the output of dir start displaying in the tab.
I only see anything when I run my executable through Qt Creator's Run menu. If I run outside Qt Creator, I see no output.
@richferrara
I really would expectQStringList args = {"/c", "start telnet"}; QProcess::startDetached("cmd", {args});to work, no? I am expecting this at least to open a terminal/console where you can interact with telnet. I do not expect it to get "destroyed". I expect it to work both inside and outside Creator. But I can't test anything.
-
I figured it out.
system("start telnet") works.
-
I figured it out.
system("start telnet") works.
@richferrara said in Launch telnet from Qt GUI application:
system("start telnet") works.
As should the code I suggested sticking with
QProcess. -
Why one needs to run a cmd.exe at all?
Simply follow the docs
QObject *parent = new QObject; ... QString program = "telnet"; QStringList arguments; arguments << "172.99.99.99"; QProcess *myProcess = new QProcess(parent); myProcess->start(program, arguments); -
Why one needs to run a cmd.exe at all?
Simply follow the docs
QObject *parent = new QObject; ... QString program = "telnet"; QStringList arguments; arguments << "172.99.99.99"; QProcess *myProcess = new QProcess(parent); myProcess->start(program, arguments);@Christian-Ehrlicher
The OP essentially claimed to have tried this and claimed it did not work:The first thing I tried was just launching telnet directly: QStringList args = {"172.99.99.99"}; // using my target IP address QProcess::startDetached("telnet.exe", {args});That is why I tried "building up" step by step with them to see what was happening. Have you tried this under Windows (which I cannot do)? The question is whether the Windows' telnet opens any kind of "terminal/console/command" window for itself, the suggestion is it does not and hence is "invisible"?
-
The windows telnet command normally runs in its own terminal window. For whatever reason, QProcess seems to only be capable of running applications in the background.
-
The windows telnet command normally runs in its own terminal window. For whatever reason, QProcess seems to only be capable of running applications in the background.
@richferrara
QProcessshould be fine. You could also try having it run, say, notepad, if you don't think it can run e.g. a UI process. Did you actually try either my code or @Christian-Ehrlicher's ? If the Windows telnet opens its own window then you ought not need to run it viacmd/start? I guess I will be quiet now as I cannot test under Windows, maybe @Christian-Ehrlicher will. -
The windows telnet command normally runs in its own terminal window. For whatever reason, QProcess seems to only be capable of running applications in the background.
@richferrara said in Launch telnet from Qt GUI application:
For whatever reason, QProcess seems to only be capable of running applications in the background.
This is wrong. Please show your code and don't use QProcess::startDetached() if you want to interact with your application.
-
@richferrara said in Launch telnet from Qt GUI application:
For whatever reason, QProcess seems to only be capable of running applications in the background.
This is wrong. Please show your code and don't use QProcess::startDetached() if you want to interact with your application.
@Christian-Ehrlicher said in Launch telnet from Qt GUI application:
and don't use QProcess::startDetached() if you want to interact with your application
Oh, really??
-
@Christian-Ehrlicher said in Launch telnet from Qt GUI application:
and don't use QProcess::startDetached() if you want to interact with your application
Oh, really??
@JonB said in Launch telnet from Qt GUI application:
@Christian-Ehrlicher said in Launch telnet from Qt GUI application:
and don't use QProcess::startDetached() if you want to interact with your application
Oh, really??
That's the whole point of this function - fire and forget
https://doc.qt.io/qt-6/qprocess.html#startDetached -
@JonB said in Launch telnet from Qt GUI application:
@Christian-Ehrlicher said in Launch telnet from Qt GUI application:
and don't use QProcess::startDetached() if you want to interact with your application
Oh, really??
That's the whole point of this function - fire and forget
https://doc.qt.io/qt-6/qprocess.html#startDetached@Christian-Ehrlicher said in Launch telnet from Qt GUI application:
and don't use QProcess::startDetached() if you want to interact with your application
and
That's the whole point of this function - fire and forget
https://doc.qt.io/qt-6/qprocess.html#startDetachedHi Christian. I am sorry but I do not agree with your statements. Under Linux at least
startDetached()causes the child process not to get killed when the parent process exits, but it is not true to say that means you do not use if you need interaction.Testing under Linux at least, where telnet does not open its own window (maybe it does under Windows, I don't know) while, say, gedit does, I see the following behaviour:
start("telnet"): runs telnet (in the background), no window, exiting Qt app kills the telnet.startDetached("telnet"): same asstart(), but exiting Qt app does not kill the telnet.start("gedit"): runs gedit, that creates its own window and interacts fine, exiting Qt app kills the gedit.startDetached("gedit"): same asstart(), but exiting Qt app does not kill the gedit.
If I want a Linux spawned telnet to be visible and have a window I have to use something like e.g.
xterm -e telnetas the command. And again that behaves as above: dies on parent exit withstart(), continues to run afterwards withstartDetached(), but same interaction in both cases.Maybe it's different under Windows and/or with telnet there, but saying that "and don't use QProcess::startDetached() if you want to interact with your application" is not the story under Linux at least. Which is why I expressed my surprise when you wrote that, as not my experience in Linux.