首页 > 解决方案 > 如何读取运行 QProcess 的输出并将特定数据保存到变量?

问题描述

我将从命令行视频播放器获取状态。当它运行时,它会输出以下行等。

[update_state]pid:0 status=PLAYING(last:PLAYING) err=0x0 curtime=4 (ms:4983) fulltime=-1 lsttime=4

所以我想读取该输出并将其实时保存到变量中。

#include "system.h"

QProcess * player = new QProcess();

QString current_status;
QString last_status;
int current_time;
int full_time;
int lsttime;

system::system(QObject *parent) : QObject(parent)
{

}

void system::start(QString url)
{
    player->start("player", QStringList(url));
}

下面更新

#include "system.h"


QProcess *player = new QProcess();
system::system(QObject *parent) : QObject(parent)
{

}
void system::start(QString url)
{
    stop();
    player->start("player", QStringList(url));
    connect(player,SIGNAL(readyReadStandardOutput()),this,SLOT(readyReadStandardOutput()));
    connect(player,SIGNAL(readyReadStandardError()),this,SLOT(readyReadStandardError()));
}

void system::readyReadStandardOutput(){
    qDebug() << player->readAllStandardOutput();
}

void system::readyReadStandardError(){
    //player->readAllStandardError();
}

标签: c++qtqt5

解决方案


推荐阅读