【问题标题】:Issue Commands to QProcess once its started启动后向 QProcess 发出命令
【发布时间】:2017-11-21 21:18:28
【问题描述】:

所以在我现在下面的代码中,它可以毫无问题地加载 gphoto2 并添加 ---shell 开关。如何再添加一行以向该 shell 发出命令我想向 shell 发出的命令是“start-preview-stream”再次感谢!

    QProcess gphoto2;
    gphoto2.start("gphoto2", QStringList() << "--shell");
    if (!gphoto2.waitForStarted()){
        qDebug("gPhoto2 didn't start properly");
        return 1;
    }

    qDebug("gPhoto2 Started Successfully");

调试展示

gPhoto2 Started Successfully

我也尝试了以下没有运气

QProcess gphoto2;
        gphoto2.start("gphoto2", QStringList() << "--shell" << "start-preview-stream");
        if (!gphoto2.waitForStarted()){
            qDebug("gPhoto2 didn't start properly");
            return 1;

        }

        qDebug("gPhoto2 Started Successfully");

【问题讨论】:

    标签: c++ qt shell terminal qt-creator


    【解决方案1】:

    如果只需要将该字符串发送到进程的标准输入流,则需要将write() 发送到QProcess

    QProcess gphoto2;
    gphoto2.start("gphoto2", QStringList() << "--shell");
    if (!gphoto2.waitForStarted()){
        qDebug("gPhoto2 didn't start properly");
        return 1;
    }
    
    qDebug("gPhoto2 Started Successfully");
    
    gphoto2.write("start-preview-stream\n");
    

    (显然,您需要添加一些适当的返回值检查等)

    【讨论】:

    • 不要用你的例子或这个过程 gphoto2; gphoto2.start("gphoto2", QStringList()
    • 您可能需要刷新输出;我没有时间编写和测试一个完整的程序,而且问题没有可以轻松复制和改编的minimal reproducible example