qprocess启动一个桌面终端
有几个问题首先是需要启动一个桌面里的终端,所以不能是bash和sh,另外就是不同桌面中的终端并不相同,需要做兼容考虑,最后一点是ghome-termial和xfce4-termial这些进程,start之后直接写标准输入也是有问题的;
Ubuntu检测当前的桌面
echo $XDG_CURRENT_DESKTOP
ls /usr/bin/*-session
neofetch
echo $DESKTOP_SESSION
ls /usr/bin/*session
https://forum.qt.io/topic/93735/start-terminal-with-command-by-qprocess/10
ubuntu环境下启动终端:
QString program = "gnome-terminal";
QStringList arguments = QStringList()<<"-e"<< adbCommand;
process->startDetached(program, arguments);
是的,这是一个不好的方法 - 例如在我的机器上这不会启动任何事情,因为我不使用(甚至没有安装)gnome-terminal但是konsole. 一般来说,唯一安全的事情是启动xterm(它应该出现在几乎任何图形安装中),尽管默认终端可能有一些 XDG 设置。
QProcess *Process = new QProcess();
QString exec = "xterm";
QStringList params;
params <<"ls";
Process->start(exec,params);
QProcess *process = new QProcess();
QString exec = "xterm";
process->start(exec);
QString l(QDir::homePath());
QProcess *process = new QProcess();
QString exec = "xterm";
QStringList params;
params <<"-hold"<<"-e"<<"htop";
process->setWorkingDirectory(l);
process->start(exec, params);
QProcess *q1 = new QProcess;;
QString k("htop");
QString l(QDir::homePath());
QStringList args;
args <<k;
q1->startDetached("xfce4-terminal",args,l);
q1->waitForStarted();
qDebug()<<args<<l;
QString l(QDir::homePath());
QProcess *process = new QProcess();
QString exec = "xterm";
QStringList params;
params << "-hold" << "-e" << "htop" ;
process->setWorkingDirectory(l);
process->start(exec, params);
https://stackoverflow.com/questions/29486638/write-command-to-gnome-terminal-with-qprocess
proc->start("gnome-terminal -e 'ls'");
*不容易。 您可以尝试直接写入 shell 或终端进程,例如 echo “ls\n” > /proc/<PID>/fd/0 但我怀疑它会起作用。 *
QProcess *proc = new QProcess;
proc->start("gnome-terminal");
proc->write("build.sh\n");// 这样不行
https://stackoverflow.com/questions/11965718/qprocess-terminal-input
两件事:1)当 start() 返回时,gnome-terminal 尚未启动。 你必须连接到proc的started()信号或者调用waitForStarted()。 2) write() 写入终端的标准输入。 那可能不会做你想要的。 (如果是这样,控制台上的“echo build.sh | gnome-terminal”也可以工作)。
windows控制台:
QDetachableProcess process;
QString program = "cmd.exe";
QStringList arguments = QStringList() << "/K" << "C:\\Program Files\\Inkscape\\python.exe";
process.setCreateProcessArgumentsModifier(
[](QProcess::CreateProcessArguments *args) {
args->flags |= CREATE_NEW_CONSOLE;
args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
});
process.start(program, arguments);
process.detach();
or
#include <cstdlib>
std::system("adb -s device shell");
If you want to just open a command shell, the command depends on the platform but it'd be something like:
std::system("cmd"); // windows
std::system("/bin/bash"); // linux, maybe osx?
// etc...
For that Python example above:
std::system("\"C:\\Program Files\\Inkscape\\python.exe\"");
// or if necessary:
std::system("cmd /K \"C:\\Program Files\\Inkscape\\python.exe\"");
// or if python is in the path, just:
std::system("python");
参考:
- 不同桌面的终端:
https://en.wikipedia.org/wiki/List_of_terminal_emulators - 检测桌面:
https://www.makeuseof.com/check-which-desktop-environment-you-are-using-linux/
https://askubuntu.com/questions/125062/how-can-i-find-which-desktop-enviroment-i-am-using - 其他
https://stackoverflow.com/questions/52264843/how-to-execute-a-shell-command-under-linux-unsing-qprocess
how-to-open-console-window-via-qprocess
https://stackoverflow.com/questions/45594248/qprocess-doesnt-show-the-command-window
https://www.faqcode4u.com/faq/585631/run-a-shell-command-via-qprocess-on-android-platform
https://blog.csdn.net/HeroGuo_JP/article/details/110133959
https://forum.qt.io/topic/93735/start-terminal-with-command-by-qprocess
- 点赞
- 收藏
- 关注作者
评论(0)