我如何使用QFtp::put上传一个文件在Qt c++

How do I use QFtp::put to upload a file in Qt C++?

本文关键字:一个 文件 Qt c++ 何使用 QFtp put      更新时间:2023-10-16

我想使用QFtp上传一个文本文件到FTP服务器。

这是我的代码:

QFile *file = new QFile("test.txt");
QFtp *ftp = new QFtp();
if(file->open(QIODevice::ReadWrite)) {
    ftp->setTransferMode(QFtp::Active);
    ftp->connectToHost(server);
    ftp->login(name, password);
    ftp->put(file, "test.txt");
    ftp->close();
}

这段代码执行后,我在ftp服务器上看不到任何东西。当我查看QFtp::put的文档时,我看到第一个参数应该是QIODevice或QByteArray。我该怎么做呢?

编辑:
所以我现在有了这段代码:

//ftp.cpp
QFile *file = new QFile("test.txt");
QFtp *ftp = new QFtp();
this->connect(ftp, SIGNAL(commandStarted(int)), SLOT(ftpCommandStarted(int)));
this->connect(ftp, SIGNAL(commandFinished(int, bool)), SLOT(ftpCommandFinished(int, bool)));  
this->connect(ftp, SIGNAL(done(bool)), SLOT(ftpDone(bool)));
this->connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)), SLOT(ftpDataTransferProgress(qint64, qint64)));
this->connect(ftp, SIGNAL(stateChanged(int)), SLOT(ftpStateChanged(int)));
if(file->open(QIODevice::ReadWrite)) {
    ftp->setTransferMode(QFtp::Active);
    ftp->connectToHost(server);
    ftp->login(name, password);
    ftp->put(file, "test.txt");
    ftp->close();
}

具有以下功能:

//ftp.h
void ftpCommandStarted(int id);
void ftpCommandFinished(int id, bool error);
void ftpDone(bool);
void ftpDataTransferProgress(qint64, qint64);
void ftpStateChanged(int);
//ftp.cpp
void EmailDialog::ftpCommandStarted(int id) {
    this->messageBox("Command Started: " + QString::number(id));
}
void EmailDialog::ftpCommandFinished(int id, bool error) {
    this->messageBox("Command Finished: " + QString::number(id) + " Error: " + (error ? "Error" : "No Error"));
}
void EmailDialog::ftpDone(bool error) {
    this->messageBox("Done " + QString(error ? "Error" : "No Error"));
}
void EmailDialog::ftpDataTransferProgress(qint64 done, qint64 total) {
    this->messageBox("Done: " + QString::number(done) + " Total: " + QString::number(total));
}
void EmailDialog::ftpStateChanged(int state) {
    QString text;
    switch (state) {
        case 0:
            text = "QFtp::Unconnected";
            break;
        case 1:
            text = "QFtp::HostLookup";
            break;
        case 2:
            text = "QFtp::Connecting";
            break;
        case 3:
            text = "QFtp::Connected";
            break;
        case 4:
            text = "QFtp::LoggingIn";
            break;
        case 5:
            text = "QFtp::Closing";
            break;
        default:
            text = "";
            break;
    }
    this->messageBox(text);
}

然而,我没有得到任何指示槽正在被调用。我没有弹出任何消息框。我哪里做错了?

您的代码片段看起来是正确的(还没有尝试编译它),所以问题可能在于该代码片段之外的某个地方。

作为对另一个答案的响应,不需要捕获信号以执行代码。调用put, close等,将这些命令排队,当它们准备好时,它们将运行,无论是否连接到信号。请参阅文档中的"详细描述"。也就是说,我强烈建议连接到信号,因为这是您为用户和调试获得反馈的方式。

至于为什么你当前的代码不能工作,我最常问的问题是:

    包含QFtp对象的类是否扩展了QObject?你的头文件包含Q_OBJECT宏吗?你有没有重新运行qmake,因为扩展QObject或添加Q_OBJECT宏?你得到任何"信号/插槽不存在"的错误在你的控制台?这通常意味着你的信号/插槽的名称中有一个错字。你的应用程序有事件循环吗?QFtp需要这样做,因为它将调用排队以异步发生。此外,所有信号和槽都需要事件循环才能工作。你通过调用QApplication的exec来启动事件循环,它通常在main()中。你有防火墙规则阻止你的ftp传输吗?您可能需要使用QFtp::Passive.

这些是我能想到的基本问题。好运!

QFtp类以异步方式传输数据。因此,按顺序调用connecToHost、put、error、currentCommand和close函数将永远不会实际执行任何命令。您需要做的是编写一个类,以便您可以使用信号和插槽。关键是在你开始传送信号后才捕捉信号。QFtp详细描述中列出的示例为您的问题提供了一些澄清/

相关文章: