我如何使用QFtp::put上传一个文件在Qt c++
How do I use QFtp::put to upload a file in Qt C++?
我想使用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详细描述中列出的示例为您的问题提供了一些澄清/
相关文章:
- 挂起和取消挂起一个文件DLL
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何将class.cpp和class.hpp编译为一个.o文件
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 如何从整个磁盘中排除要装载的一个文件
- 如何将模板和非模板函数放在一个文件中
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 使用 scope 运算符 (::) 引用另一个文件中的类
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 为什么我不能从同一目录中的另一个文件中 #include 我的类?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- C++ 如何将两个 makefile 对象目标规则(位于另一个文件夹中)合并到一个目标/规则中?
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- c++ 提升如果 .extension() == " "这意味着这个文件是一个文件夹?
- 是否可以在 c++ 中创建一个文件并为其提供属性以将其隐藏?(在 Ubuntu 上)
- 我需要编写一个程序来读取一个文件,该文件将输出所有唯一的整数,如果已经看到整数,它将被关闭
- 如何在标准c ++中流式传输/读取二进制文件的中间部分并写入另一个文件?