简体   繁体   English

将qstring设置为qprocess正确

[英]set qstring to qprocess properly

I am trying to set a QString to Qprocess to execute it in Qt. 我试图将QString设置为Qprocess以在Qt中执行它。 My problem is that I can't get it working although it seems fine from the QProcess Documentation . 我的问题是,尽管从QProcess文档看来还不错,但我无法使其正常运行。 I wanted to do this in a pushbutton press, so my code looked at first like the following (thumbs up, the process is a ROS Command): 我想通过按一下按钮来执行此操作,因此我的代码首先如下所示(竖起大拇指,该过程是ROS命令):

void Viatic_Interface::gazebo_launch_world()
{
    QString program = "roslaunch";
    QStringList arguments;
    arguments << " wifibot_gazebo " << ui->txt_world->text();

    QProcess *myProcess = new QProcess();
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    myProcess->setProcessEnvironment(env);
    myProcess->start(program, arguments);
    myProcess->waitForStarted(-1);
}

and that didn't work. 那没有用。 It looks like sth is going in getting the QString from the LineEdit. 看起来似乎正在从LineEdit获取QString。 Just to check if my command works, I tried to set the command as a one line command and it worked : 只是为了检查我的命令是否有效,我尝试将命令设置为单行命令并且它有效:

void Viatic_Interface::gazebo_launch_world()
{
    QString program = "roslaunch wifibot_gazebo wifibot_gazebo.launch";

    QProcess *myProcess = new QProcess();
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    myProcess->setProcessEnvironment(env);
    myProcess->start(program);
    myProcess->waitForStarted(-1);
}

Am I missing something ?? 我错过了什么吗?


This was solved by removing the spaces and adding a QString cast, like this : 这是通过删除空格并添加QString类型转换解决的,如下所示:

void Viatic_Interface::gazebo_launch_world()
{
    QString program = "roslaunch";
    QStringList arguments;
    arguments << "wifibot_gazebo" << (QString)ui->txt_world->text();

    QProcess *myProcess = new QProcess();
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    myProcess->setProcessEnvironment(env);
    myProcess->start(program, arguments);
    myProcess->waitForStarted(-1);
}

found no explanation why... 找不到解释为什么...

Thanks for the help anyway ! 无论如何,谢谢您的帮助!

Cheers, 干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM