Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read data of a website
Forum Updated to NodeBB v4.3 + New Features

Read data of a website

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 7 Posters 1.4k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Linz
    wrote on 12 Jun 2021, 11:11 last edited by
    #1

    Hello qt-forum,

    what do I want to do?

    • read in data from a website
    • every 5 seconds
    • plot it

    Where do I struggle?

    • read in the data since I am a beginner I have no idea how to realize it. It should be possible with QNetwork, but everything I tried didn't work :/

    Hopefully you have an idea, would help me a lot :)
    You can see how the website looks in the picture. !Bildschirmfoto 2021-06-12 um 12.55.25.png

    Thanks!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 12 Jun 2021, 12:31 last edited by mrjj 6 Dec 2021, 12:32
      #2

      Hi

      It depends on how the server works but you can try to send a GET and see.

      #include <QDebug>
      #include <QtNetwork/QNetworkAccessManager>
      #include <QtNetwork/qnetworkreply.h>
      
      
      void MainWindow::on_pushButton_4_clicked()
      {
      
          QEventLoop loop;
          QNetworkAccessManager nam;
          QNetworkRequest req(QUrl("http://YOURURL.com"));
          QNetworkReply *reply = nam.get(req);
          connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
          loop.exec();
          QByteArray buffer = reply->readAll();
      
          qDebug() << "what did i get" << buffer;
      
      

      and see what you get.

      Do note. this is just test code. it lacks error handling (a must have in real app) and we don't want to use
      QEventLoop but instead just hook up to signals.
      However, its just to see if you do in fact get the text string as your image shows.

      1 Reply Last reply
      2
      • L Offline
        L Offline
        Linz
        wrote on 12 Jun 2021, 13:09 last edited by Linz 6 Dec 2021, 13:09
        #3

        Thanks for your help :) I get the following message:

        Execution of PAC script at "http://wpad/wpad.dat" failed: The operation could not be completed. (NSURLErrorDomain-Fehler -1003.)
        what did i get "<?xml version="1.0" encoding="UTF-8"?>\n<r>\n<f>49.988</f>\n<z> 12.06.2021 13:00:55</z>\n<p>228.6</p>\n<dt>0</dt>\n</r>\n"

        But there is my data: 49.998 12.06.2021 13:00:55 226.8 0

        Just need to find a way to filter it. That the execution failed shouldn't bother me?

        A 1 Reply Last reply 12 Jun 2021, 13:13
        0
        • L Linz
          12 Jun 2021, 13:09

          Thanks for your help :) I get the following message:

          Execution of PAC script at "http://wpad/wpad.dat" failed: The operation could not be completed. (NSURLErrorDomain-Fehler -1003.)
          what did i get "<?xml version="1.0" encoding="UTF-8"?>\n<r>\n<f>49.988</f>\n<z> 12.06.2021 13:00:55</z>\n<p>228.6</p>\n<dt>0</dt>\n</r>\n"

          But there is my data: 49.998 12.06.2021 13:00:55 226.8 0

          Just need to find a way to filter it. That the execution failed shouldn't bother me?

          A Offline
          A Offline
          artwaw
          wrote on 12 Jun 2021, 13:13 last edited by
          #4

          @Linz Hi, I don't know about the error (I personally would investigate that).
          To parse XML you can use https://doc.qt.io/qt-5/qxmlstreamreader.html

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          3
          • L Offline
            L Offline
            Linz
            wrote on 12 Jun 2021, 13:30 last edited by Linz 6 Dec 2021, 14:42
            #5

            I fixed the error, something with my proxy was off ^^

            Do you have an idea how I can convert the QByteArray buffer to a char? Would It make way more easy to access the values I need.

            V 1 Reply Last reply 12 Jun 2021, 15:14
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 12 Jun 2021, 15:02 last edited by
              #6

              Hi,

              No need to convert anything.

              You can access the internal data using the QByteArray::data function.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • L Linz
                12 Jun 2021, 13:30

                I fixed the error, something with my proxy was off ^^

                Do you have an idea how I can convert the QByteArray buffer to a char? Would It make way more easy to access the values I need.

                V Offline
                V Offline
                VRonin
                wrote on 12 Jun 2021, 15:14 last edited by VRonin 6 Dec 2021, 15:14
                #7

                @Linz said in Read data of a website:

                Do you have an idea how I can convert the QByteArray buffer to a char? Would It make way more easy to access the values I need.

                Don't do this.

                @artwaw said in Read data of a website:

                To parse XML you can use https://doc.qt.io/qt-5/qxmlstreamreader.html

                Do this!

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                4
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 12 Jun 2021, 18:26 last edited by
                  #8

                  I somehow missed the XML part.

                  My fellows are correct, use QXmlStreamReader.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • L Offline
                    L Offline
                    Linz
                    wrote on 14 Jun 2021, 15:42 last edited by
                    #9

                    Sorry for bothering you: I read the documentation for QXmlStreamReader, but I still have no clue how to filter the data. Maybe I am still missing the basics too much to understand what I need...

                    If I refer to the picture I want to save the first value 49.986, the second value 132.3 and the third value 0 in an array(s) of type double (to plot it later on).

                    But which function could do this for me?
                    I tried something like:
                    void QXmlStreamReader::addData(const QByteArray &buffer);

                    but it doesn't work. I could filter it easily if it would have been an array of char :/

                    JonBJ 1 Reply Last reply 14 Jun 2021, 15:56
                    0
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on 14 Jun 2021, 15:55 last edited by
                      #10

                      higher level:

                      curl
                      wget

                      1 Reply Last reply
                      1
                      • L Linz
                        14 Jun 2021, 15:42

                        Sorry for bothering you: I read the documentation for QXmlStreamReader, but I still have no clue how to filter the data. Maybe I am still missing the basics too much to understand what I need...

                        If I refer to the picture I want to save the first value 49.986, the second value 132.3 and the third value 0 in an array(s) of type double (to plot it later on).

                        But which function could do this for me?
                        I tried something like:
                        void QXmlStreamReader::addData(const QByteArray &buffer);

                        but it doesn't work. I could filter it easily if it would have been an array of char :/

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 14 Jun 2021, 15:56 last edited by
                        #11

                        @Linz
                        You just want to parse input XML. I don't think you'll particularly be needing QXmlStreamReader::addData(). You're just wanting to read elements and access values.

                        There is a Qt example at https://doc.qt.io/qt-5/qtxml-streambookmarks-example.html. But for you you're only interested in the XbelReader::readXBEL() part. It should get you started on how to read through an XML document.

                        1 Reply Last reply
                        1
                        • V Offline
                          V Offline
                          VRonin
                          wrote on 14 Jun 2021, 16:05 last edited by VRonin
                          #12
                          const QByteArray replyData = reply->readAll();
                          QXmlStreamReader xmlreader(replyData);
                          bool rStarted = false;
                          while (!xmlreader.atEnd() && !xmlreader.hasError()) {
                              switch(xmlreader.readNext()){
                              case QXmlStreamReader::StartElement:
                                  if(xmlreader.name()==QLatin1String("r"))
                                      rStarted = true;
                                  else if(rStarted && xmlreader.name()==QLatin1String("f"))
                                      qDebug() << "f" << xmlreader.readElementText();
                                  else if(rStarted && xmlreader.name()==QLatin1String("z"))
                                      qDebug() << "z" << xmlreader.readElementText();
                                  else if(rStarted && xmlreader.name()==QLatin1String("p"))
                                      qDebug() << "p" << xmlreader.readElementText();
                                  else if(rStarted && xmlreader.name()==QLatin1String("dt"))
                                      qDebug() << "dt" << xmlreader.readElementText();
                              break;
                              case QXmlStreamReader::EndElement:
                                  if(xmlreader.name()==QLatin1String("r"))
                                      rStarted = false;
                              break;
                              default:
                              break;
                              }
                          }
                          

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          L 1 Reply Last reply 14 Jun 2021, 17:23
                          5
                          • V VRonin
                            14 Jun 2021, 16:05
                            const QByteArray replyData = reply->readAll();
                            QXmlStreamReader xmlreader(replyData);
                            bool rStarted = false;
                            while (!xmlreader.atEnd() && !xmlreader.hasError()) {
                                switch(xmlreader.readNext()){
                                case QXmlStreamReader::StartElement:
                                    if(xmlreader.name()==QLatin1String("r"))
                                        rStarted = true;
                                    else if(rStarted && xmlreader.name()==QLatin1String("f"))
                                        qDebug() << "f" << xmlreader.readElementText();
                                    else if(rStarted && xmlreader.name()==QLatin1String("z"))
                                        qDebug() << "z" << xmlreader.readElementText();
                                    else if(rStarted && xmlreader.name()==QLatin1String("p"))
                                        qDebug() << "p" << xmlreader.readElementText();
                                    else if(rStarted && xmlreader.name()==QLatin1String("dt"))
                                        qDebug() << "dt" << xmlreader.readElementText();
                                break;
                                case QXmlStreamReader::EndElement:
                                    if(xmlreader.name()==QLatin1String("r"))
                                        rStarted = false;
                                break;
                                default:
                                break;
                                }
                            }
                            
                            L Offline
                            L Offline
                            Linz
                            wrote on 14 Jun 2021, 17:23 last edited by
                            #13

                            @VRonin Thanks a lot! Where do you define readElementText() since I get the Error:
                            Error: use of undeclared identifier 'readElementText'

                            I tried to define it as QString readElementText() in the header, but it doesn't seem to work.

                            V 1 Reply Last reply 14 Jun 2021, 17:25
                            0
                            • L Linz
                              14 Jun 2021, 17:23

                              @VRonin Thanks a lot! Where do you define readElementText() since I get the Error:
                              Error: use of undeclared identifier 'readElementText'

                              I tried to define it as QString readElementText() in the header, but it doesn't seem to work.

                              V Offline
                              V Offline
                              VRonin
                              wrote on 14 Jun 2021, 17:25 last edited by
                              #14

                              Sorry forgot to add xmlreader. amended now

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              1 Reply Last reply
                              1

                              1/14

                              12 Jun 2021, 11:11

                              • Login

                              • Login or register to search.
                              1 out of 14
                              • First post
                                1/14
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved