QT + Gaode Map Web Service API development - search input prompt demoqt UI layout, build request URL, slot function and QT network access

Posted by Drebin on Sun, 16 Jan 2022 03:11:48 +0100

QT-UI layout

The simple arrangement in the early stage is good, and it may be adjusted in the future

The following is a brief description of the following figure: lineEdit is used to input information, ListWidget is used to display the final prompt information, and TextBrowser is used to print the obtained XML file, which can be deleted later

Build request URL

According to Google, a service example is given https://restapi.amap.com/v3/assistant/inputtips? For the standard request header, city = XXX & keywords = XXX & output = XML & key = XXX is the request parameter. Since our key value is fixed and the output is specified as XML first, it is difficult to enter the city message, so the changed parameter is only keywords, from the UI Get in lineedit. Two functions are created to build the request URL as follows.

QString searchInputTips::spliceUrl()
{
	QString city = "city=020&";  //020 for Guangzhou
	QString inputKey = ui.lineEdit->text();
	QString keywords = "keywords=" + inputKey.toUtf8() + "&";
	QString url = urlHead + "?" + city + keywords + urlTail;
	return url;
}

void searchInputTips::initFixedUrl()
{
	key = "<!!!Yours key!!!>";
	urlHead = "https://restapi.amap.com/v3/assistant/inputtips";
	urlTail = "output=xml&key=" + key;
}

Slot function

Next we give UI lineEdit binds a slot function, which will be called whenever the lineEdit content changes.

searchInputTips::searchInputTips(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	initFixedUrl();

	connect(ui.lineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(whenLineEditChange(const QString&)));
}


void searchInputTips::whenLineEditChange(const QString&) {
	spliceUrl();
}

In debug mode, enter any information into lineEdit and observe that the url can be spliced correctly. Copy the url and access it in the browser to get the XML file.

QT network access

With the correct URL, we need to use QT's QNetworkAccessManager, QNetworkReply and QNetworkRequest to obtain network data. Qt network access simple tutorial.

#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>

QByteArray searchInputTips::getReplay(const QString& urlstr)
{
	//Qt get network data operation code
	QNetworkAccessManager netManger;
	QUrl url(urlstr);
	QNetworkRequest netRequest(url);
	QNetworkReply* reply = netManger.get(netRequest);

	//Wait for the request to complete
	QEventLoop loop;
	QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
	loop.exec();

	//Wrong judgment
	if (reply->error() != QNetworkReply::NoError) {
		qDebug() << "Happend [reply] false:" << reply->error();
		return "reply error";
	}

	//return
	QByteArray result = reply->readAll();
	delete reply;
	return result;
}

Get the QByteArray directly in UI Displayed in textbrowse.

void searchInputTips::whenLineEditChange(const QString&) {
	QByteArray xmlByteArray = getReplay(spliceUrl());
	ui.textBrowser->setText(xmlByteArray);
}

Running the program can see that we will get a new XML every time we input, but the format seems not beautiful. We can use QDomDocument to organize the XML format (of course, there may be some better ways to organize, but I'm not sure)

#include <QDomDocument>

QString searchInputTips::formatXml(const QByteArray& xml)
{
	//QDomDocument can be formatted
	QDomDocument doc;
	doc.setContent(xml);
	return doc.toString();
}

The running results should be as follows

In the next chapter, we parse the obtained XML and add a little improvement mechanism.

Possible problems

1.QT Chinese garbled code

Since the characters entered by the user contain Chinese, remember to use QString::toUtf8() when splicing URL s.

Of course, if some strings are Chinese, QStringLiteral("Chinese") can be used to avoid.

Recommended blog - Chinese garbled code

2. Unable to open source file

Please check XML and NetWork in project - properties - QT project settings QT modules

Topics: C++ Qt5