【问题标题】:how to use QRegExp to grep the last line of output of process?如何使用 QRegExp grep 进程的最后一行输出?
【发布时间】:2015-08-23 13:17:32
【问题描述】:

我正在使用 QRegExp 通过 grep 输出 QProcess 的最后一行

QString str (process->readAllStandardOutput());

现在我想阅读 str 的最后一行并在状态栏中显示 请问怎么才能只得到最后一行:(

【问题讨论】:

  • 为什么要使用正则表达式? str.mid(str.lastIndexOf('\n')+1) 左右。
  • 使用 rx.indexIn(l​​ine); 让它工作;
  • 实际上我需要从字符串中获取一些特定信息,这就是我选择 QRegExp 的原因;)

标签: c++ qt qt4


【解决方案1】:

使用 QRegExp 得到了一个可行的解决方案

代码示例

QString line(download->readAllStandardOutput());
QString progress;
QString timeRemaining;
if (line.contains(QString("[download]"), Qt::CaseInsensitive)) {
    QRegExp rx("(\\d+\\.\\d+%)");
    timeRemaining = line.right(5);
    rx.indexIn(line);
    if(!rx.cap(0).isEmpty()) {
        progress = rx.cap(0);
        progress.chop(3);
        ui->downloadProgressBar->setValue(progress.toInt());
        ui->downloadProgressBar->setAlignment(Qt::AlignCenter);
        ui->downloadProgressBar->setFormat(timeRemaining);
        ui->statusBar->showMessage(line);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    相关资源
    最近更新 更多