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)
相关推荐
Fireworkitte1 小时前
Ubuntu 系统 tar 包源码安装 Nginx
linux·nginx·ubuntu
ElendaLee2 小时前
笔记本电脑Windows+Ubuntu 双系统,Ubuntu无法挂载Windows的硬盘 报错问题解决
linux·运维·ubuntu
永不停转2 小时前
详谈 QLayout::SizeConstraint 和 QSizePolicy 对 QWidget 尺寸的影响
c++·qt
QtSeeker2 小时前
Ubuntu22.04安装VMware Tools
linux·ubuntu
wzb563 小时前
Ubuntu 的 apt-get 强制使用 IPv4 网络
ubuntu
王廷胡_白嫖帝6 小时前
Qt网络速度测试工具开发教程 - 从零开始构建网络测速应用
开发语言·网络·qt
John_ToDebug15 小时前
Chrome 内置扩展 vs WebUI:浏览器内核开发中的选择与实践
前端·c++·chrome
小猪写代码16 小时前
解释一下,Linux,shell,Vmware,Ubuntu,以及Linux命令和shell命令的区别
linux·ubuntu
源代码杀手16 小时前
大模型微调训练资源占用查询:Windows 10 查看 NVIDIA 显卡GPU状态教程(替代 Ubuntu 下 watch nvidia-smi)
linux·windows·ubuntu
孙霸天19 小时前
Ubuntu20系统上离线安装MongoDB
数据库·mongodb·ubuntu·备份还原