因此,我使用 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());
}