In this example, we will learn how to create a simple currency converter using Qt, with the help of an external service provider called Fixer.io
.
Make yourself a currency converter with these simple steps:
.pro
) and add the network module to our project:QT += core gui network
mainwindow.ui
and remove the menu bar, tool bar, and status bar from the UI.1
:readOnly
checkbox located in the property pane:clicked()
signal as the selection and click OK. Qt Creator will now automatically add a slot function for you to both mainwindow.h
and mainwindow.cpp
.mainwindow.h
and make sure the following headers are being added to the top of the source file:#include <QMainWindow> #include <QDoubleValidator> #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> #include <QJsonDocument> #include <QJsonObject> #include <QDebug> #include <QMessageBox>
finished()
:private slots:
void on_convertButton_clicked();
void finished(QNetworkReply* reply);
private
label:private: Ui::MainWindow *ui; QNetworkAccessManager* manager; QString targetCurrency;
mainwindow.cpp
this time. We will add several currency shortcodes to both the combo boxes in the class constructor. We also set a validator to the line edit widget on the left so that it can only accept inputs that are numbers. Lastly, we also initialize the network access manager and connect its finished()
signal to our finished()
slot function:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QStringList currencies; currencies.push_back("EUR"); currencies.push_back("USD"); currencies.push_back("CAD"); currencies.push_back("MYR"); currencies.push_back("GBP"); ui->currencyFrom->insertItems(0, currencies); ui->currencyTo->insertItems(0, currencies); QValidator *inputRange = new QDoubleValidator(this); ui->amountFrom->setValidator(inputRange); manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*))); }
void MainWindow::on_convertButton_clicked() { if (ui->amountFrom->text() != "") { ui->convertButton->setEnabled(false); QString from = ui->currencyFrom->currentText(); QString to = ui->currencyTo->currentText(); targetCurrency = to; QString url = "http://api.fixer.io/latest?base=" + from + "&symbols=" + to; QNetworkRequest request= QNetworkRequest(QUrl(url)); manager->get(request); } else { QMessageBox::warning(this, "Error", "Please insert a value."); } }
finished()
signal is triggered:void MainWindow::finished(QNetworkReply* reply) { QByteArray response = reply->readAll(); qDebug() << response; QJsonDocument jsonResponse = QJsonDocument::fromJson(response); QJsonObject jsonObj = jsonResponse.object(); QJsonObject jsonObj2 = jsonObj.value("rates").toObject(); double rate = jsonObj2.value(targetCurrency).toDouble(); if (rate == 0) rate = 1; double amount = ui->amountFrom->text().toDouble(); double result = amount * rate; ui->amountTo->setText(QString::number(result)); ui->convertButton->setEnabled(true); }
Similar to the previous example we saw, which uses an external program to achieve a specific task, this time we use an external service provider who provided us with an open Application Programming Interface (API) that is free for all and easy to use.
This way, we don't have to think about the method to retrieve the latest currency rate. Instead, the service provider has already done the job for us and we just have to send a polite request and ask for it. Then, we just wait for the response from their server and process the data according to our intended purposes.
There are quite a few different service providers you can choose from besides Fixer.io
(http://fixer.io). Some are free but without any advanced features; some provide you with additional functionalities, although they come at a premium price. Some of these alternatives are Open Exchange Rate (https://openexchangerates.org), Currencylayer (https://currencylayer.com), Currency API (https://currency-api.appspot.com), XE Currency Data API (http://www.xe.com/xecurrencydata), and Jsonrates (http://jsonrates.com).
Besides currency exchange rates, you can also use this method to do other more advanced tasks that are perhaps too complicated to do by yourself, or are simply impossible to access unless you use the services provided by specialists, for example, programmable Short Message Service (SMS) and voice services, web analytics and statistic generation, online payment gateways, and the list goes on. Most of these services are not free, but you can easily achieve those functions in minutes without even setting up the server infrastructure, backend system, and whatnot; it's definitely the cheapest and fastest way to get your product up and running without much hassle.
3.144.15.154