如何 link QProgressBar 到终端进程百分比?

How to link a QProgressBar to a Terminal process percentage?

所以我使用 Qt 设计了一个非常简单的对话框,用于从 youtube 下载视频并将它们转换为 mp4 或 mp3,一切都使用 youtube-dl 命令(我使用system() 调用 youtube-dl)。是的,我是 Linux 用户。 它工作正常,但我想在我的 UI 中显示下载进度,就像我直接从那里调用 youtube-dl 时在终端中显示的那样。 任何对整个代码的建设性批评将不胜感激。

//ytdialog.h
#ifndef YTDIALOG_H
#define YTDIALOG_H
#include <iostream>
using namespace std;
#include <QDialog>

class QPushButton;
class QLineEdit;
class QLabel;

class ytDialog : public QDialog
{
  Q_OBJECT
  public:
    ytDialog(QWidget *parent = 0);
  private slots:
    void videoOutput(); //download the video that corresponds to the link in   link(QLineEdit)
    void audioOutput(); //download the video and converts it to mp3
    void enableButtons(); //enable the buttons when link is not empty
  private:
    QLabel *linkLabel; //Just a Label
    QPushButton *video; //Button to Download video
    QPushButton *audio; //Button to Download video and convert it to mp3
    QLineEdit *link; //Input link
};

#endif

//ytdialog.cpp **Just In case You need it**
#include <QtWidgets>

#include "ytdialog.h"

ytDialog::ytDialog(QWidget *parent)
      : QDialog(parent)
{
  linkLabel = new QLabel("Link: ");
  link = new QLineEdit;
  video = new QPushButton("Video Output");
  audio = new QPushButton("Audio Output");

  link->setMinimumWidth(400);
  video->setEnabled(false);
  audio->setEnabled(false);

  connect(link, SIGNAL(textChanged(const QString &)),
      this, SLOT(enableButtons()));
  connect(video, SIGNAL(clicked()),
      this, SLOT(videoOutput()));
  connect(audio, SIGNAL(clicked()),
      this, SLOT(audioOutput()));

  QHBoxLayout *linkLayout = new QHBoxLayout;
  linkLayout -> addWidget(linkLabel);
  linkLayout -> addWidget(link);

  QHBoxLayout *buttonsLayout = new QHBoxLayout;
  buttonsLayout -> addWidget(video);
  buttonsLayout -> addWidget(audio);

  QVBoxLayout *main = new QVBoxLayout;
  main -> addLayout(linkLayout);
  main -> addLayout(buttonsLayout);

  setLayout(main);
  setWindowTitle("Download Youtube");
}
void ytDialog::enableButtons()
{
   if (!link->text().isEmpty())
   {
     video->setEnabled(true);
     audio->setEnabled(true);
   }
   else
   {
     video->setEnabled(false);
     audio->setEnabled(false);
   }
}
void ytDialog::videoOutput()
{
  string cmd = "youtube-dl -o '/home/rodrigo/Videos/%(title)s.%(ext)s' " + link-    >text().toStdString();
  system(cmd.c_str());
}

void ytDialog::audioOutput()
{
  string cmd = "youtube-dl -x --audio-format mp3 -o '/home/rodrigo/Music/%(title)s.%    (ext)s' " + link->text().toStdString();
  system(cmd.c_str());
}

您可以使用QProcess来执行命令。然后你可以使用 readyReadStandardOutput 信号读取槽中的命令输出消息并处理文本以提取进度百分比:

在你的构造函数中:

process = new QProcess();

connect (process, SIGNAL(readyReadStandardOutput()), this, SLOT(readyRead()));
connect (process, SIGNAL(finished (int, QProcess::ExitStatus)), this, SLOT(finished()));

videoOutput 插槽:

void ytDialog::videoOutput()
{
  string cmd = "youtube-dl -o '/home/rodrigo/Videos/%(title)s.%(ext)s' " + link-    >text().toStdString();
  process.start("/bin/sh", QStringList()<< "-c" << cmd);
}

readyRead 插槽:

ytDialog::readyRead()
{
  if (!process)
    return;

  QString str = process->readAllStandardOutput();

  //Extract the progress percentage and emit a signal to update the progress bar
  ...
}