GUI在数据采集期间使用QProcess冻结

时间:2017-05-30 14:34:20

标签: qt user-interface command-line qprocess external-application

问题定义:我有一个名为 runSensor 的外部应用程序,它与传感器通信。传感器通过 UDP 发送数据。使用终端, runSensor 有两个参数可与传感器进行通信以进行数据采集:startstop

一旦到达终端,我呼叫:$ runSensor start,示例输出如下:

[Time 07:20:11:000]: Device PoweredOn.
[Time 07:20:11:010]: x=1.231, y=-0.022, z=0.001
[Time 07:20:11:015]: x=1.235, y=-0.024, z=0.001
[Time 07:20:11:020]: x=1.241, y=-0.024, z=0.002
[Time 07:20:11:025]: x=1.258, y=-0.027, z=0.002

我需要使用 QT -UI致电startstop。为此,我有一个 QDialog 如下:

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->socketHandler         = std::make_shared<udpHandler>();
    this->runSensorStartProcess = std::make_shared<QProcess>();
    this->runSensorStopProcess  = std::make_shared<QProcess> ();

    //--------------------------
    // SIGNAL - SLOT
    //--------------------------
    connect(ui->startButton, SIGNAL(clicked()), this,  SLOT(onStartButtonClicked()));
    connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(onStopButtonClicked()));
    connect(this->socketHandler.get(), SIGNAL(sendUdpContent(QString)), this, SLOT(updateMessageBrowser(QString)));
    connect(this->runSensorStartProcess.get(), SIGNAL(readyReadStandardError()), this, SLOT(printError()));
}

我使用QProcess来调用 runSensor 应用程序的开始停止。当我按下 GUI 的开始按钮时,数据采集开始,但 GUI 冻结,我无法点击停止按钮。< / p>

代码提取:以下是我实施 start 按钮点击的方式:

void Dialog::onStartButtonClicked()
{ 
    this->runSensorStartProcess->start("start");
    //this->runSensorStartProcess->waitForFinished(-1);

    //--------------------------
    // udp socket handler starts picking data and write it into a file
    //--------------------------

    if (!this->runSensorStartProcess->waitForStarted())
    {
        qWarning() << "Warning: Cannot start Cygwin process!";
    }
}

停止按钮点击实现类似:

void Dialog::onStopButtonClicked()
{
    if(this->runSensorStartProcess.get() != NULL)
    {
        this->runSensorStartProcess->close();
    } 
    this->runSensorStopProcess->start("stop");

    if (!this->runSensorStopProcess->waitForStarted())
    {
        qWarning() << "Warning: Cannot stop Cygwin!";
    }
}

问题:

  1. 如何在runSensorStartProcess启动后保持GUI响应?
  2. 如何按需停止该过程(实际上是通过启动runSensorStartProcess)?
  3. thread
  4. 需要单独的runSensorStartProcess吗?

1 个答案:

答案 0 :(得分:1)

  1. 不要使用任何waitForXxx方法。这就是全部。是的,就这么简单。

    请注意,对于每种waitForXxx方法,您都可以附加信号,从而对您正在寻找的事件作出反应。

  2. Aren&#t; t QProcess::killQProcess::terminate你需要什么?

  3. 从不。另请参阅answer 1answer 2