ubuntu qt 运行命令行

文章目录

1.C++实现

下面是封装好的C++头文件,直接调用run_cmd_fun()即可。

cpp 复制代码
#ifndef GET_CMD_H
#define GET_CMD_H

#endif // GET_CMD_H
#include <iostream>
#include<QString>
using namespace std;

//system("gnome-terminal --window -e 'bash -c \"pwd;ls;exec bash\"' --tab -e 'bash -c \"pwd; exec bash\"'");
QString win_cmd = "gnome-terminal --window -e 'bash -c \"";  // 运行窗口的命令
QString tab_cmd = " --tab -e 'bash -c \""; //运行标签的命令
QString pause_cmd = ";exec bash\"'";  //终端等待的命令
QString source_path = "source /opt/ros/noetic/setup.bash ;"; //添加环境变量
QString Qcmd = "";

void run_cmd_fun(QString cmd1)
{ //get one cmd
    Qcmd = "";
    Qcmd.append(win_cmd);
    Qcmd.append(source_path);
    Qcmd.append(cmd1);
    Qcmd.append(pause_cmd);
    string cmd = Qcmd.toStdString();
    cout << cmd << endl;
    system(cmd.c_str());
}

void run_cmd_fun(QString cmd1, QString cmd2, int input_time=1)
{ //get two cmd
    //The time set
    Qcmd = "";
    QString pause_t = "sleep ";
    pause_t.append(QString::number(input_time));
    pause_t.append(";");
    //The  first cmd set
    Qcmd.append(win_cmd);
    Qcmd.append(cmd1);
    Qcmd.append(pause_cmd);

    //The  second cmd set
    Qcmd.append(tab_cmd);
    Qcmd.append(pause_t);
    Qcmd.append(cmd2);
    Qcmd.append(pause_cmd);

    //The  cm1 + cm2 --> string cmd
    string cmd = Qcmd.toStdString();
    system(cmd.c_str());
}

void run_cmd_fun(QString cmd1, QString cmd2, QString cmd3, int input_time=1)
{ //get three cmd
    Qcmd = "";
    QString pause_t = "sleep ";
    pause_t.append(QString::number(input_time));
    pause_t.append(";");
    //The  first cmd set
    Qcmd.append(win_cmd);
    Qcmd.append(cmd1);
    Qcmd.append(pause_cmd);

    //The  second cmd set
    Qcmd.append(tab_cmd);
    Qcmd.append(pause_t);
    Qcmd.append(cmd2);
    Qcmd.append(pause_cmd);

    //The  third cmd set
    Qcmd.append(tab_cmd);
    Qcmd.append(pause_t);
    Qcmd.append(pause_t);
    Qcmd.append(cmd3);
    Qcmd.append(pause_cmd);

    //The  cmd1 + cmd2 + cmd3 --> cmd
    string cmd = Qcmd.toStdString();
    system(cmd.c_str());
}


void run_cmd_fun(QString cmd1, QString cmd2, QString cmd3, QString cmd4, int input_time=1)
{ //get four cmd
    Qcmd = "";
    //The time set
    QString pause_t = "sleep ";
    pause_t.append(QString::number(input_time));
    pause_t.append(";");

    //The first cmd set
    Qcmd.append(win_cmd);
    Qcmd.append(cmd1);
    Qcmd.append(pause_cmd);

    //The second cmd set
    Qcmd.append(tab_cmd);
    Qcmd.append(pause_t);
    Qcmd.append(cmd2);
    Qcmd.append(pause_cmd);

    //The third cmd set
    Qcmd.append(tab_cmd);
    Qcmd.append(pause_t);
    Qcmd.append(pause_t);
    Qcmd.append(cmd3);
    Qcmd.append(pause_cmd);

    //The fourth cmd set
    Qcmd.append(tab_cmd);
    Qcmd.append(pause_t);
    Qcmd.append(pause_t);
    Qcmd.append(pause_t);
    Qcmd.append(cmd4);
    Qcmd.append(pause_cmd);

    // cmd1 + cmd2 + cmd3 + cmd4 = cmd
    string cmd = Qcmd.toStdString();
    system(cmd.c_str());
}

注意:想要生成双击直接运行的程序,需要添加:

bash 复制代码
QMAKE_LFLAGS += -no-pie

补充切换不同页面;

cpp 复制代码
ui->stackedWidget->setCurrentIndex(3);

2.python实现

直接调用:run_cmd("roscore","rostopic list", 2)

python 复制代码
openvino_path = "source /opt/intel/openvino/bin/setupvars.sh;"  # 添加的一些虚拟环境
ros_path = "source ~/.bashrc;"+openvino_path 


win_cmd = "gnome-terminal --window -e 'bash -c \""+ros_path
tab_cmd = " --tab -e 'bash -c \""+ros_path
end_cmd = ";exec bash\"'"

def run_win(cmd):
    # 运行的第一个窗口终端
    cmd = win_cmd+cmd+end_cmd
    return cmd

def run_tab(cmd, t):
    # 运行的标签终端
    delay_t = "sleep {};".format(str(t))
    cmd = tab_cmd+delay_t+cmd+end_cmd
    return cmd

def run_cmd(cmds, t = 2):
    cmd  = run_win(cmds[0])
    for i in range(1, len(cmds)):
        cmd = cmd + run_tab(cmds[i], t*i)
    print(cmd)
    os.system(cmd)
相关推荐
信鸽爱好者14 小时前
一人多台电脑办公模式:windows 电脑A远程桌面操作ubuntu电脑B
linux·运维·ubuntu
OSMeteor16 小时前
在 Hyper-V 中搭建固定 IP、固定网关、可上网且与宿主机互通的 Ubuntu 环境
网络协议·tcp/ip·ubuntu
初级炼丹师(爱说实话版)1 天前
Ubuntu服务器配置docker
服务器·ubuntu·docker
bellus-1 天前
Ubuntu 26.04 Miniforge3 安装与配置完整指南
linux·运维·ubuntu
今天AI了吗1 天前
OpenCode AI 编程:Ubuntu 24.04 环境安装与使用指南
linux·人工智能·ubuntu
用什么都重名2 天前
Ubuntu 24.04 安装 NVIDIA 驱动:解决 “NVIDIA-SMI has failed“ 报错全记录
linux·运维·ubuntu
Quz2 天前
QML Tumbler 样式定制:渐变、高亮与 3D 滚轮效果
qt
bosins2 天前
Ubuntu 日常最常用的文件操作场景和对应指令
linux·服务器·ubuntu
PinoLio2 天前
鲁班猫5-实战篇之虚拟机运行yolov5
yolo·ubuntu
啦啦啦!2 天前
虚拟机如何接入Codex并配置中转站(vscode插件版)
linux·vscode·ubuntu·编辑器