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. Retrieve weather forecast
Qt 6.11 is out! See what's new in the release blog

Retrieve weather forecast

Scheduled Pinned Locked Moved General and Desktop
19 Posts 4 Posters 11.6k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    I'm new to Qt. I'm trying to make a Qt GUI application to retrieve weather forecast information but I don't know where to start.

    Can anyone guide me to do this please?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      If you new to Qt, start with "tutorials":http://qt-project.org/doc/qt-4.8/tutorials.html.

      About your subject:
      Have you already found service which can provide you with weather forecast information? Start with that. Find one. There is many services wich provides this kind of information through simple REST request. For example Norway Weather Institute(search for yr.no API).
      Also, mostly all such services wants Longitude & Latitude of place the weather forecast you are looking for. So you will need to convert real address into geographical point. It is also easy to do, google provides simple REST API for that.

      All that information you can get with "QNAM":http://qt-project.org/doc/qt-4.8/qnetworkaccessmanager.html get request. (check examples provided in QNAM documentation)

      Search for information I've wrote about, choose right service, start coding. It is easy task. If you will get in troubles with your code, ask for help with code snipped where you have got problem. Happy coding!

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        thanks a lot, appreciate your quick reply. :)

        1 Reply Last reply
        0
        • I Offline
          I Offline
          issam
          wrote on last edited by
          #4

          I think your project is somewhat similar to an RSS reader !
          If so, you have to use XML.

          http://www.iissam.com/

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            I decided to use QXmlStreamReader to read a simple xml code retrieve from "http://rss.weatherzone.com.au/?u=12994-1285&lt=aploc&lc=9388&obs=1&fc=1&warn=1"

            but when i'm trying to debug the program exited with code 0xc0000139

            I've been digging around on Google and still can't find the answer to my problem

            Whenever I used anything involving Qtnetworkd4.dll it raise that problem. Not sure if my code in .pro is wrong or not though.

            Here's the code sniplet of part of my project:

            test.pro

            @QT += core gui network xml

            TARGET = Clock
            TEMPLATE = app

            SOURCES += main.cpp
            mainwindow.cpp

            HEADERS += mainwindow.h

            FORMS += mainwindow.ui@

            a push button in mainwindow:
            @void MainWindow::on_pushButton_3_clicked()
            {
            QNetworkAccessManager networkAccessManager = new QNetworkAccessManager(this);
            connect(networkAccessManager, SIGNAL(finished(QNetworkReply
            )),this, SLOT(slotFinished(QNetworkReply*)));
            QUrl url("http://rss.weatherzone.com.au/?u=12994-1285&lt=aploc&lc=9388&obs=1&fc=1&warn=1");
            QNetworkReply *reply = networkAccessManager->get(QNetworkRequest(url));connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(slotError( QNetworkReply::NetworkError)));
            QXmlStreamReader reader( reply );
            reader.readNext();
            while (!reader.atEnd()) {
            if (reader.isStartElement()) {
            if (reader.name() == "title") {
            reader.readNext();
            if (reader.text() == "Brisbane weather forecast") {
            //blah blah blah
            }
            }
            }
            }
            }@

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              by the way I'm using QtCreator 2.4.1 base on Qt 4.7.4(32 bit)

              and one more question, when i'm trying to use the QtNetworkAccessManager I can't do
              @#include <QtNetwork>@

              but I have to
              @#include <QtNetwork/QtNetworkAccessManager>@

              the same for QtNetworkReply, QtNetworkRequest

              anyone know the reason to this?

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                You should read reply in slotFinished().

                @
                void MainWindow::on_pushButton_3_clicked()
                {

                QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this);
                connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(slotFinished(QNetworkReply*)));
                
                QUrl url("http://rss.weatherzone.com.au/?u=12994-1285&lt=aploc&lc=9388&obs=1&fc=1&warn=1");
                
                QNetworkReply *reply = networkAccessManager->get(QNetworkRequest(url));connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(slotError( QNetworkReply::NetworkError)));
                

                }

                void MainWindow::slotFinished(QNetworkReply* reply)
                {
                if(reply->error() != QNetworkReply::NoError)
                {
                qCritical() << reply->errorString();
                return;
                }
                QXmlStreamReader reader( reply );
                reader.readNext();
                while (!reader.atEnd()) {
                if (reader.isStartElement()) {
                if (reader.name() == "title") {
                reader.readNext();
                if (reader.text() == "Brisbane weather forecast") {
                //blah blah blah
                }
                }
                }
                }

                }
                @

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  It raised the same error, the 0xc0000139 thing. Keep getting that every time i use QtNetwork

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    When I tried to run the file in my debug folder with all the needed dll i got

                    Entry Point Not Found

                    The procedure entry point _Z15qIsEffectiveTLDRK7QString could not be located in the dynamic link library QtCored4.dll.

                    and if I try to debug it it raised

                    The program has unexpectedly finished.
                    C:\Users\DeepBlue\Documents\Qt\Clock-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\debug\Clock.exe exited with code -1073741511

                    I just know that -1073741511 = 0xc0000139.

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      I don't see possible error here. It is somewhere else... Set some breakpoints and debug your app step-by-step

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        Well I managed to clean up the Build Environment and it works now, turn out I was using the wrong environment variables.

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          now when I tried to debug i get 2 errors
                          Object::connect: No such slot MainWindow::slotFinished(QNetworkReply*) in ..\Clock\mainwindow.cpp:755
                          Object::connect: (receiver name: 'MainWindow')
                          Object::connect: No such slot MainWindow::slotError( QNetworkReply::NetworkError) in ..\Clock\mainwindow.cpp:757
                          Object::connect: (receiver name: 'MainWindow')

                          1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

                            Ok I know what to do now, thanks a lot AcerExtensa

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #14

                              My code after I clean up a bit is

                              @void MainWindow::on_pushButton_3_clicked()
                              {

                              QNetworkAccessManager * manager = new QNetworkAccessManager(this);
                              connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotFinished(QNetworkReply*)));
                              QUrl url("http://rss.weatherzone.com.au/?u=12994-1285&lt=aploc&lc=9388&obs=1&fc=1&warn=1");
                              
                              manager->get(QNetworkRequest(url));
                              

                              }

                              void MainWindow::slotFinished(QNetworkReply* reply)
                              {
                              if(reply->error() != QNetworkReply::NoError)
                              {
                              qCritical() << reply->errorString();
                              return;
                              }
                              QXmlStreamReader reader( reply );
                              reader.readNext();
                              while (!reader.atEnd()) {
                              if (reader.isStartElement()) {
                              if (reader.name() == "title") {
                              reader.readNext();
                              if (reader.text() == "Brisbane weather forecast") {
                              //blah blah blah
                              qDebug()<< reader.text();
                              }
                              }
                              }
                              }

                              }@

                              When I debug, whenever I click the button the program freeze up. Where is the problem--?

                              1 Reply Last reply
                              0
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #15

                                lol silly me, I didn't put

                                @reader.readNext();@

                                in the while loop, and I keep checking my slot connections

                                1 Reply Last reply
                                0
                                • ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #16

                                  -Hm, it can be only because of while loop. but it should not take more then some seconds...-

                                  1 Reply Last reply
                                  0
                                  • ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on last edited by
                                    #17

                                    "
                                    Tuesday

                                    Mostly sunny
                                    14°C - 25°C
                                    Wednesday

                                    Mostly sunny
                                    14°C - 26°C
                                    Thursday

                                    Mostly sunny
                                    13°C - 28°C
                                    Friday

                                    Mostly sunny
                                    15°C - 32°C
                                    "

                                    I'm almost there but I need a way to split this string and my head is going dumb now. How can i get only the Days and the weather status?

                                    1 Reply Last reply
                                    0
                                    • ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #18

                                      the problem is, your choosen service returns RSS feed with forecast as simple html text in [CDATA]... So, you need to parse it yourself... For example with "QRegExp":http://qt-project.org/doc/qt-4.8/qregexp.html parse days first(there is only 7 days), after that parse temperature, it may always have format like:
                                      +/- <min 1 digit - max 2digits>°<ignore symbols> - <ignore symbols> +/- <min 1 digit - max 2 digit>°

                                      1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        johndev
                                        Banned
                                        wrote on last edited by
                                        #19
                                        This post is deleted!
                                        1 Reply Last reply
                                        0

                                        • Login

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