Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QNetworkReply problem with error



  • Hello !

    I would like make an application for create link with my clound. For this, i would like use the FTP protocole. For this, i use the library Network but, i can't generate the error message and i can't verify the connection. Whith good value, the transfer don't work and i don't have any message and when i use bas value, the application don't creat error message.

    I put my code (i have only this code):

    Ftp.cpp :

    #include "ftp.h"
    
    void Ftp()
    {
    
    }
    
    void Ftp::launchFtp()
    {
        QFile *data = new QFile("C:/Users/Meropy/Documents/build-test_ftp-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/toto.txt",this);
    
        QNetworkAccessManager ftp;
        QUrl url("ftp://127.0.0.2/test");
        url.setUserName("toto");
        url.setPassword("1234");
        url.setPort(21);
    
    
         if (data->open(QIODevice::ReadOnly)) {
    
             printf("ok1\n");
    
             QEventLoop ev;
    
             QNetworkRequest req(url);
             printf("ok2\n");
             reply = ftp.put(req,data);
             reply->error();
             printf("ok3\n");
    
    
             printf("ok4\n");
    
             connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(error2(QNetworkReply::NetworkError)));
             //connect(reply, SIGNAL(uploadProgress(qint64, qint64)),this,SLOT(uploadProgress(qint64, qint64)));
             reply->error();
         }
    }
    
    void Ftp::uploadProgress(qint64 done, qint64 total)
    {
        printf("je suis ici");
        double val = done/total * 100;
        if (done < total)
        {
            printf("%lf" , val);
        }
    }
    
    void Ftp::error2(QNetworkReply::NetworkError code)
    {
        printf("ici : %d\n",code);
    }
    
    
    

    Ftp.h :

    #ifndef FTP_H
    #define FTP_H
    
    #include <QObject>
    #include <QtCore>
    #include <QDebug>
    
    #include <QtNetwork>
    
    class Ftp : public QObject
    {
         Q_OBJECT
    private:
        QNetworkReply *reply;
    
    public:
        Ftp();
        void launchFtp();
        void error_bis();
    
    
    public slots:
        void uploadProgress(qint64 done, qint64 total);
        void error2(QNetworkReply::NetworkError);
    
    
    };
    
    #endif // FTP_H
    
    

    thank's your for any answer.



  • @vin212
    I don't really know what your exact problem is, but:

    • What are your statements reply->error(); supposed to do? They do nothing.

    • You seem to attempt connect the "error" signal after you have made requests. that's no good, needs connecting before.

    • SIGNAL(error(QNetworkReply::NetworkError)): I see no signal named error(). You do not check the return result of the connect(). If you would change over to New Signal Slot Syntax your life, and ours, would be better. I suspect there is no such signal and it would tell you so.

    • You choose to declare a QEventLoop ev; but you do not use it. Your QNetworkAccessManager ftp; will go out of scope at the end of launchFtp() but I am not sure you are finished with it.



  • @JonB

    . it's just a test for "force" the error but it's don't work.
    . it's special signal generate by the QNetworkReply (if i understand)
    . ok, i try this
    . QEventLoop ev; don't have any utility, i juste missing to delete



  • @vin212
    I'm sorry but your answers either don't make sense or are incorrect. I suggest you go through these items and address them. In particular, use the documentation to look up the methods and signals you are trying to use.



  • #include "ftp.h"
    
    void Ftp()
    {
    
    }
    
    void Ftp::launchFtp()
    {
        QFile *data = new QFile("C:/Users/Meropy/Documents/build-test_ftp-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/toto.txt",this);
    
        QNetworkAccessManager ftp;
        QUrl url("ftp://127.0.0.2/test");
        url.setUserName("toto");
        url.setPassword("1234");
        url.setPort(21);
    
         if (data->open(QIODevice::ReadOnly)) {
    
             printf("ok1\n");
    
             QNetworkRequest req(url);
             printf("ok2\n");
             reply = ftp.put(req,data);
             printf("ok3\n");
    
    
             printf("ok4\n");
    
             bool toto = connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(error2(QNetworkReply::NetworkError)));
              printf("%d \n", toto);
             //connect(reply,&QNetworkReply::error,this,&Ftp::error2);
             //connect(reply, SIGNAL(uploadProgress(qint64, qint64)),this,SLOT(uploadProgress(qint64, qint64)));
         }
    }
    
    void Ftp::uploadProgress(qint64 done, qint64 total)
    {
        printf("je suis ici");
        double val = done/total * 100;
        if (done < total)
        {
            printf("%lf" , val);
        }
    }
    
    void Ftp::error2(QNetworkReply::NetworkError code)
    {
        printf("ici : %d\n",code);
    }
    
    
    

    the first change.
    If i change : connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(error2(QNetworkReply::NetworkError))); to connect(reply,&QNetworkReply::error,this,&Ftp::error2); i have an erro message



  • @vin212
    I'm going to leave you to this. I have given you information about your problems/what to do, and you have done nothing about them, or some of them. I'm not going to repeat to myself, it's up to you whether you choose to act on them or ignore them.

    P.S.
    One hint:

    to connect(reply,&QNetworkReply::error,this,&Ftp::error2); i have an erro message

    Yeah, that's exactly what I thought and why you need to change. Did you act on the error message or just ignore it?



  • @JonB

    i deleted the usless ligne. On my code, i collected the boolean create by "connect" i have the value "true" .
    I use the signal "error" on the class QNetworkReply .



  • @vin212 said in QNetworkReply problem with error:

    I use the signal "error" on the class QNetworkReply .

    Which signal? Here's the list: https://doc.qt.io/qt-5/qnetworkreply.html#signals



  • @vin212 said in QNetworkReply problem with error:

    I use the signal "error" on the class QNetworkReply .

    Which is why I asked you to check the docs for the functions you use. For yourself. And I think the "error message" you receive when you use the new style connect syntax would have told you this?



  • @JonB
    OK, i understand better, this part, it'is issue to internet and i don't know realy how the code works. i change a little my code with :

     bool test = connect(reply,&QNetworkReply::errorOccurred,this,&Ftp::error2);
    printf("test = %d\n",test);
    test = connect(reply,&QNetworkReply::sslErrors,this,&Ftp::error3);
    printf("test = %d\n",test);
    

    and is not better ^^'. I'm so sorry for the incomprehension.



  • @vin212
    Well at least this is better! :) Though you still say nothing about where you put this, so it's anyone's guess what still does not work.

    Qt's network example for "Fortune" are the place to start. I would suggest you look at Fortune Client Example. I would copy that code, or at least the parts of it which deal with setting up the connection and the signals. Get that working first as your "base", and go from there.


Log in to reply