Traditional Culture Encyclopedia - Weather inquiry - What does Qt5 mean by the time error of obtaining weather data?
What does Qt5 mean by the time error of obtaining weather data?
This function mainly provides web pages from all over the world with the help of China Weather Network, and intercepts the character strings on the web pages by using the network class of Qt.
Step 1: Establish two related Qt network class objects.
QNetworkAccessManager * manager
QNetworkReply * reply;
QString city; //The string used to get the location.
Bad weather; //A string used to get the weather.
Public slot:
void get weather(QNetworkReply * reply weather); //Create a new slot here, which will be used in step 3.
1
2
three
four
five
six
1
2
three
four
five
six
Step 2: Get the web pages of various places provided by China Weather Network.
QString str("/data/ 10 10 10 100 . html "); //The number string inside represents the code of the city.
1
1
Step 3: Set the two objects of step 1.
manager = new QNetworkAccessManager(parent);
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(getWeather(QNetworkReply *)); //When a response is obtained, it is associated with the corresponding slot.
QString str("/data/ 10 10 10 100 . html ");
Manager-> get(QNetworkRequest(QUrl(str)); //Request a response from this webpage.
1
2
three
four
1
2
three
four
Step 4: Intercept the string.
void dream weather::get weather(QNetworkReply * replay weather)
{
QTextCodec * codec = QTextCodec::codec forname(" utf8 ");
QString str = codec-& gt; toUnicode(replayweather-& gt; readAll()); //Read all obtained requests.
//The following is the operation of intercepting web page strings. Why do you want to do it? You can look at the web page and get to know this bird.
str.replace(QString("{ ",QString("+"));
QStringList weather list = str . split('+');
str = weather list . at(list . count()- 1);
str.replace(QString("} "),QString(" ");
str.replace(QString("\ " "),QString(" ");
weatherlist=str.split(',');
for(int I = 0; I< weatherlist. count (); i++)
{
QString st
Li;
ST = weather list . at(I);
Li = ST . split(':');
if(li.first()=="city ")
city = Li . at( 1); //Get the local string
if(li.first()=="weather 1 ")
weather 1 = Li . at( 1); //Get the local weather string
}
Launch completed (true);
}
//The relevant information obtained will be stored in the string in step 1. That web page provides a lot of information and can intercept a lot of things.
- Previous article:How to prevent sandstorm
- Next article:What's the weather like in Gannan from April to May? Preferably from Xiahe County! thank you
- Related articles
- It's very cold now. What should we eat?
- A joke about spring
- Chengdu 165438+ What to wear in October?
- What's the weather like in Lushan at the end of March?
- What kind of pencil is used to draw the weather map?
- When will Zhuhai Lotus bloom and haizhu lake Lotus bloom in 2022?
- What does the typhoon blue warning stand for?
- Who played the roles of France and India in Kangxi's incognito visit?
- What kind of weather conditions must artificial rainfall have in drought?
- Linyi weather forecast 15 lazy days