I want to get file from server



  • Hi
    I want to get file from server ( download )
    I write this code and get error:

    void MainWindow::on_pushButton_clicked()
    {
    QNetworkAccessManager downloadmanager;
    QNetworkRequest dl2;
    downloadmanager.get(dl2.setUrl(ui->textEdit->toPlainText.toString()));
    }
    

    error: '((MainWindow*)this)->MainWindow::ui->Ui::MainWindow::<anonymous>.Ui_MainWindow::textEdit->QTextEdit::toPlainText' does not have class type
    downloadmanager.get(dl2.setUrl(ui->textEdit->toPlainText.toString()));
    ^



  • I change my code but get error again :(

    void MainWindow::on_pushButton_clicked()
    {
    QNetworkAccessManager downloadmanager;
    QNetworkRequest dl2;
    t = ui->textEdit->toPlainText();
    downloadmanager.get(dl2.setUrl(QUrl::url(t)));
    }
    
    

  • Moderators

    Hi! void QNetworkRequest::setUrl(const QUrl &url) returns void, but QNetworkReply *QNetworkAccessManager::get(const QNetworkRequest &request) takes a const QNetworkRequest &.



  • Thanks @Wieland
    I get error from this code:

    void MainWindow::on_pushButton_clicked()
    {
    t = ui->textEdit->toPlainText();
    QNetworkAccessManager::get(QNetworkRequest::setUrl(QUrl::url()));
    }
    
    

    C:\Users\armin\Documents\DownloadManager\mainwindow.cpp:22: error: cannot call member function 'QString QUrl::url(QUrl::FormattingOptions) const' without object
    QNetworkAccessManager::get(QNetworkRequest::setUrl(QUrl::url()));
    ^


  • Lifetime Qt Champion

    Hi,

    You are calling a normal function as it was static. You need to have a QUrl object if you want to call the url method on it.


  • Moderators

    @Armin said in I want to get file from server:

    QNetworkRequest

    As @Wieland already told you setUrl() does not return anything. But QNetworkAccessManager::get() expects a QNetworkRequest.
    And as @SGaist said calling QUrl::url() is just wrong as url() is not static (and what do you actually expect it to return?).
    Why not just

    void MainWindow::on_pushButton_clicked()
    {
    QNetworkAccessManager *downloadmanager = new QNetworkAccessManager(this);
    QNetworkRequest dl2;
    t = ui->textEdit->toPlainText();
    dl2.setUrl(QUrl::url(t));
    downloadmanager.get(dl2);
    }
    

    You should allocate QNetworkAccessManager on the heap via new because else it will disappear as soon as on_pushButton_clicked() finishes. Important: downloadmanager.get(dl2) is asynchronous! You should really read the documentation.



  • Thanks @SGaist and Thanks @jsulm
    @jsulm I put your and replace with my code but i get error:

    C:\Users\armin\Documents\DownloadManager\mainwindow.cpp:22: error: no matching function for call to 'QUrl::url(QString&)'
    dl2.setUrl(QUrl::url(t));
    ^

    C:\Users\armin\Documents\DownloadManager\mainwindow.cpp:23: error: request for member 'get' in 'downloadmanager', which is of pointer type 'QNetworkAccessManager*' (maybe you meant to use '->' ?)
    downloadmanager.get(dl2);
    ^


  • Moderators

    @Armin Well, QUrl does not have a get() method, you just use the constructor (that's why a said you should read documentation):

    dl2.setUrl(QUrl(t));
    

    The second error message actually tells you what is wrong:

    downloadmanager->get(dl2);
    


  • Really thanks @jsulm

    I don't have any error but i replace this code with your code and i don't get error , My new code isn't wrong?

    void MainWindow::on_pushButton_clicked()
    {
        QNetworkAccessManager downloadmanager;
    
    QNetworkRequest dl2;
    t = ui->textEdit->toPlainText();
    dl2.setUrl(QUrl(t));
    downloadmanager.get(dl2);
    }
    

  • Moderators

    @Armin Are you aware that downloadmanager will be destroyed as soon as void MainWindow::on_pushButton_clicked() terminates? I already said that you should allocate it on the heap using new. downloadmanager.get(dl2) is assynchronous - that means the result will be available later, after downloadmanager.get(dl2) finishes.
    Do it like this:

    QNetworkAccessManager *downloadmanager = new QNetworkAccessManager(this);
    

  • Moderators

    @Armin And how do you want to get the result? Currently you do not connect any slots to the signals. You will need http://doc.qt.io/qt-5/qnetworkaccessmanager.html#finished


  • Lifetime Qt Champion

    To add to @jsulm, make your downloadmanager a member of MainWindow and initialise it in your constructor, otherwise you are going to uselessly create new QNetworkAccessManager.


Log in to reply
 

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