downloader.cpp

来自「这里包含了一些QT的小例子,很有价值的,希望对大家有帮助」· C++ 代码 · 共 59 行

CPP
59
字号
#include <qmessagebox.h>#include "downloader.h"Downloader::Downloader(const QUrl &url){    if (url.protocol() != "ftp") {        QMessageBox::warning(0, tr("Downloader"),                             tr("Protocol must be 'ftp'."));        emit finished();        return;    }    int port = 21;    if (url.hasPort())        port = url.port();    connect(&ftp, SIGNAL(done(bool)),            this, SLOT(ftpDone(bool)));    connect(&ftp, SIGNAL(listInfo(const QUrlInfo &)),            this, SLOT(listInfo(const QUrlInfo &)));    ftp.connectToHost(url.host(), port);    ftp.login(url.user(), url.password());    ftp.cd(url.path());    ftp.list();}void Downloader::listInfo(const QUrlInfo &urlInfo){    if (urlInfo.isFile() && urlInfo.isReadable()) {        QFile *file = new QFile(urlInfo.name());        if (!file->open(IO_WriteOnly)) {            QMessageBox::warning(0, tr("Downloader"),                                 tr("Error: Cannot open file "                                    "%1:\n%2.")                                 .arg(file->name())                                 .arg(file->errorString()));            emit finished();            return;        }        ftp.get(urlInfo.name(), file);        openedFiles.push_back(file);    }}void Downloader::ftpDone(bool error){    if (error)        QMessageBox::warning(0, tr("Downloader"),                             tr("Error: %1.")                             .arg(ftp.errorString()));    for (int i = 0; i < (int)openedFiles.size(); ++i)        delete openedFiles[i];    emit finished();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?