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

[solved]QNetwork and QLabel



  • Hi all, I'm sorry if my topic in the wrong forum.
    I have a problem. I try to get text (answer) from remote server and write it to QLabel but my code doesn't works. Here is it:
    @static QNetworkAccessManager am;
    QUrl url("https://api.vk.com/method/audio.get");
    url.addQueryItem("access_token","some_code");
    QNetworkRequest request(url);
    QNetworkReply* reply = am.get(request);
    label1->setText(reply->read());@

    Please, help me to solve my problem, if you can



  • Check the example at Qt Project wiki for "downloading data from URL":http://qt-project.org/wiki/Download_Data_from_URL The example usage shows how to download an image an to set it on a QPushButton. You can easily modify it to download text and to set it to a QLabel.



  • You tried to read "reply" even though the request isn't finished yet.

    create a slot first:
    @//.h
    public slots: //you can use private slots
    void download();
    @

    @//.cpp
    void className::download() //className is whatever you are using
    {
    label1->setText(reply->read());
    }@

    then use signal-slot mechanism:
    @static QNetworkAccessManager am;
    QUrl url("https://api.vk.com/method/audio.get");
    url.addQueryItem("access_token","some_code");
    QNetworkRequest request(url);
    QNetworkReply* reply = am.get(request);
    QObject::connect(reply,SIGNAL(finished()),this,SLOT(download()))@



  • Thanks. I will try it



  • I had the same problem, worked with me.



  • [quote author="dotK" date="1359023688"]I had the same problem, worked with me.[/quote]

    The 1st reply helped me. Maybe it helps you too


Log in to reply