Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use QFtp::put to upload a file in Qt C++?
    primarykey
    data
    text
    <p>I would like to use QFtp to upload a text file to a FTP server. </p> <p>This is the code I have :</p> <pre><code>QFile *file = new QFile("test.txt"); QFtp *ftp = new QFtp(); if(file-&gt;open(QIODevice::ReadWrite)) { ftp-&gt;setTransferMode(QFtp::Active); ftp-&gt;connectToHost(server); ftp-&gt;login(name, password); ftp-&gt;put(file, "test.txt"); ftp-&gt;close(); } </code></pre> <p>After this code executes, I don't see anything on my ftp server. When I look at the documentation for QFtp::put, I see that the first parameter should be a QIODevice or QByteArray. How should I do this?</p> <p>Edit:<br> So I have this code now:</p> <pre><code>//ftp.cpp QFile *file = new QFile("test.txt"); QFtp *ftp = new QFtp(); this-&gt;connect(ftp, SIGNAL(commandStarted(int)), SLOT(ftpCommandStarted(int))); this-&gt;connect(ftp, SIGNAL(commandFinished(int, bool)), SLOT(ftpCommandFinished(int, bool))); this-&gt;connect(ftp, SIGNAL(done(bool)), SLOT(ftpDone(bool))); this-&gt;connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)), SLOT(ftpDataTransferProgress(qint64, qint64))); this-&gt;connect(ftp, SIGNAL(stateChanged(int)), SLOT(ftpStateChanged(int))); if(file-&gt;open(QIODevice::ReadWrite)) { ftp-&gt;setTransferMode(QFtp::Active); ftp-&gt;connectToHost(server); ftp-&gt;login(name, password); ftp-&gt;put(file, "test.txt"); ftp-&gt;close(); } </code></pre> <p>with these functions:</p> <pre><code>//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-&gt;messageBox("Command Started: " + QString::number(id)); } void EmailDialog::ftpCommandFinished(int id, bool error) { this-&gt;messageBox("Command Finished: " + QString::number(id) + " Error: " + (error ? "Error" : "No Error")); } void EmailDialog::ftpDone(bool error) { this-&gt;messageBox("Done " + QString(error ? "Error" : "No Error")); } void EmailDialog::ftpDataTransferProgress(qint64 done, qint64 total) { this-&gt;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-&gt;messageBox(text); } </code></pre> <p>However, I don't get any indication that the slots are being called. I don't have any message boxes popping up. What am I doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload