Following up on a great answer, I am trying to get around an -apparanently common- error **C2536 : cannot specify explicit initializer for arrays**
for line in line: QLineEdit * edits[3] = {&lineEditName, &lineEditGender, &lineEditRegion};
I have read other solutions (e.g. 1, 2) but do not appear to apply in my case. I am using Qt Creator 4.1.0 with Qt 5.7.0, MSVC 2013 in Windows with C++. Any suggestions are welcome.
On an extra note, and in the interest of better understanding Qt with C++, could someone point to a different way -instead of using arrays- for holding the new parsed json results?
Below you can see the full code from that answer (the line in question is in mainwindow.h
):
main.cpp
// https://github.com/KubaO/stackoverflown/tree/master/questions/into-mainwin-39643510
#include "mainwindow.h"
#include "controller.h"
int main(int argc, char *argv[])
{
QApplication app{argc, argv};
MainWindow ui;
Controller ctl;
QTimer timer;
timer.start(5*1000);
QObject::connect(&timer, &QTimer::timeout, &ctl, &Controller::get);
QObject::connect(&ctl, &Controller::busy, &ui, [&]{ ui.setState(MainWindow::Loading); });
QObject::connect(&ui, &MainWindow::request, &ctl, &Controller::get);
QObject::connect(&ctl, &Controller::error, &ui, [&]{ ui.setState(MainWindow::Error); });
QObject::connect(&ctl, &Controller::values, &ui, &MainWindow::setFields);
ui.show();
return app.exec();
controller.h
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QtNetwork>
class Controller : public QObject {
Q_OBJECT
QNetworkAccessManager manager{this};
QNetworkRequest request;
Q_SLOT void onReply(QNetworkReply *);
public:
explicit Controller(QObject * parent = nullptr);
Q_SLOT void get();
Q_SIGNAL void busy();
Q_SIGNAL void error(const QString &);
Q_SIGNAL void values(const QString & name, const QString & gender, const QString & region);
};
#endif // CONTROLLER_H
controller.cpp
#include "controller.h"
Controller::Controller(QObject *parent) : QObject(parent)
{
QUrlQuery query;
query.addQueryItem("amount", "1");
query.addQueryItem("region", "United States");
QUrl url("http://uinames.com/api/");
url.setQuery(query);
request = QNetworkRequest(url);
connect(&manager, &QNetworkAccessManager::finished, this, &Controller::onReply);
}
void Controller::onReply(QNetworkReply * reply) {
if (reply->error() != QNetworkReply::NoError) {
emit error(reply->errorString());
manager.clearAccessCache();
} else {
//parse the reply JSON and display result in the UI
auto jsonObject = QJsonDocument::fromJson(reply->readAll()).object();
auto fullName = jsonObject["name"].toString();
fullName.append(" ");
fullName.append(jsonObject["surname"].toString());
emit values(fullName, jsonObject["gender"].toString(), jsonObject["region"].toString());
}
reply->deleteLater();
}
void Controller::get() {
emit busy();
manager.get(request);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets>
class MainWindow : public QWidget {
Q_OBJECT
QFormLayout layout{this};
QLineEdit lineEditName;
QLineEdit lineEditGender;
QLineEdit lineEditRegion;
QPushButton button{"Get Name"};
QLineEdit * edits[3] = {&lineEditName, &lineEditGender, &lineEditRegion};
public:
enum State { Normal, Loading, Error };
explicit MainWindow(QWidget * parent = nullptr);
Q_SLOT void setFields(const QString & name, const QString & gender, const QString & region);
Q_SLOT void setState(State);
Q_SIGNAL void request();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
for(auto edit : edits) edit->setReadOnly(true);
layout.addRow("Name:", &lineEditName);
layout.addRow("Gender:", &lineEditGender);
layout.addRow("Region:", &lineEditRegion);
layout.addRow(&button);
connect(&button, &QPushButton::clicked, this, &MainWindow::request);
}
void MainWindow::setFields(const QString & name, const QString & gender, const QString & region) {
setState(Normal);
lineEditName.setText(name);
lineEditGender.setText(gender);
lineEditRegion.setText(region);
}
void MainWindow::setState(MainWindow::State state) {
if (state == Normal) {
for (auto edit : edits) edit->setEnabled(true);
button.setEnabled(true);
}
else if (state == Loading) {
for (auto edit : edits) edit->setEnabled(false);
button.setEnabled(false);
}
else if (state == Error) {
for (auto edit : edits) edit->setText("Error...");
button.setEnabled(true);
}
}