FTP access with Qt 5.3



  • Okay, I've tried to avoid asking this question but I simply cannot wrap my head around how to work with a ftp site and use QNetworkAccessManager. Qt's documentation explains how to use the classes is pieces. But doesn't have a good example that shows how to use them together.

    Part of the confusion lies in the fact that apparently in Qt 5 they no longer support QFtp--or at least discourage us from using it. But every example I can find uses QFtp!

    So my basic starting point is, I wanted to log into my ftp site and just get a list of files from a given directory, how is this done with QNetworkAccessManager? Here's what I've done so far. But clearly, this isn't executing a pwd command. Not sure where that even occurs...

    // Header
    @#ifndef NETWORKMANAGER_H
    #define NETWORKMANAGER_H

    #include <QObject>
    #include <QNetworkAccessManager>

    class QNetworkReply;
    class QNetworkAccessManager;
    class NetworkManager : public QObject
    {
    Q_OBJECT
    private:
    QNetworkAccessManager m_netman;

    public:
    explicit NetworkManager(QObject *parent = 0);
    ~NetworkManager();

    bool ftpTester();

    signals:

    public slots:
    void replyFinished(QNetworkReply *_reply);

    };

    #endif // NETWORKMANAGER_H
    @

    // Cpp
    @#include "NetworkManager.h"
    #include <QNetworkRequest>
    #include <QNetworkReply>
    #include <QUrl>
    #include <Underworld/Base/NStatus.h>

    NetworkManager::NetworkManager(QObject parent) :
    QObject(parent)
    {
    connect(&m_netman, SIGNAL(finished(QNetworkReply
    )),
    this, SLOT(replyFinished(QNetworkReply*)));
    }

    NetworkManager::~NetworkManager()
    {
    }

    bool NetworkManager::ftpTester()
    {
    QNetworkRequest request(QUrl("ftp://<username>:<password>@ftp.mysite.com/sweetFTP/"));
    m_netman.get(request);
    return false;
    }

    void NetworkManager::replyFinished(QNetworkReply *_reply)
    {
    if( _reply->error())
    {
    NStatus::nowError("Unable to get to ftp site.");
    NStatus::nowError(_reply->errorString());
    } else {
    NStatus::nowNote(_reply->header(QNetworkRequest::ContentTypeHeader).toString());
    }
    _reply->deleteLater();
    }@

    any direction, tutorials I can read or any code samples would be appreciated!


  • Moderators

    For the time being you can only use put() and get() when accessing ftp with QNetworkAccessManager ie. upload or download a file assuming you know the path beforehand. To use commands like "PWD" or "LIST" you would have to go a level down, to a QTcpSocket I suppose.


  • Moderators

    There is also "QtFtp":https://qt.gitorious.org/qt/qtftp/source/80823b53d2025d9c0c15075c325ee0c2e364551b: which is referred "here":http://qt-project.org/forums/viewthread/24466
    You can see in the example how to use QNetworkAccessManager as QFtp used to be. Alternatively, you can use the easy route and use the compatibility class directly.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.