Я пытаюсь получить значения из "void readJson ();"Функция с использованием геттера.
void readJson () - функция, которую я создал для установки значений для соответствующих переменных, объявленных в моем классе
Я пытался установить каждую переменную как общедоступную, но, похоже, она тоже не работает.
void readJson()
{
...
QJsonArray json_array = json_doc.array();
/*Instantiate QJsonArray class object*/
TrainServiceDisruptionData tsds;
for (int i = 0; i < json_array.count(); ++i) {
tsds.setLine(json_array.at(i).toObject().value("line"));
}
//Train Lines
nsline nsline;
ewline ewline;
neline neline;
ccline ccline;
dtline dtline;
orline orline;
//Initializing values
int nsl = 0, ewl = 0, nel = 0, ccl = 0, dtl = 0, orl = 0;
int totalline = tsds.getLine().size();
for (int i = 0; i < totalline; ++i) {
if (tsds.getLine().at(i) == "North-South Line")
{
nsl += 1;
}
else if (tsds.getLine().at(i) == "East-West Line")
{
ewl += 1;
}
else if (tsds.getLine().at(i) == "North-East Line")
{
nel += 1;
}
else if (tsds.getLine().at(i) == "Circle Line")
{
ccl += 1;
}
else if (tsds.getLine().at(i) == "Downtown Line")
{
dtl += 1;
}
else
{
orl += 1;
}
}
nsline.setlines(nsl);
ewline.setlines(ewl);
neline.setlines(nel);
ccline.setlines(ccl);
dtline.setlines(dtl);
orline.setlines(orl);
qDebug() << "<Respective Train Breakdown Count>" << endl
<< "North-South Line: " << nsline.getlines() << endl
<< "East-West Line: " << ewline.getlines() << endl
<< "North-East Line: " << neline.getlines() << endl
<< "Circle Line: " << ccline.getlines() << endl
<< "Downtown Line: " << dtline.getlines() << endl
<< "Other Lines: " << orline.getlines() << endl
<< "Total Count: " << totalline << endl;
}
else
{
qDebug() << "file does not exists" << endl;
exit(1);
file.close();
}
}
... The codes below are where I use getter function to retrieve those values from setter function declared in readJson function...
void MainGUI::showpiechartbtnReleased()
{
nsline nsline;
ewline ewline;
neline neline;
ccline ccline;
dtline dtline;
orline orline;
int nsl = nsline.getlines();
int ewl = ewline.getlines();
int nel = neline.getlines();
int ccl = ccline.getlines();
int dtl = dtline.getlines();
int orl = orline.getlines();
//Plot pie chart data
QPieSeries *series = new QPieSeries();
series->append("North-South Line", nsl);
series->append("East-West Line", ewl);
series->append("North-East Line", nel);
series->append("Circle Line", ccl);
series->append("Downtown Line", dtl);
//series->append("Other Lines", orl);
//no idea why qt pie chart unable to display 6 data
qDebug() << "<Respective Train Breakdown Count 2>" << endl
<< "North-South Line: " << nsline.getlines() << endl
<< "East-West Line: " << ewline.getlines() << endl
<< "North-East Line: " << neline.getlines() << endl
<< "Circle Line: " << ccline.getlines() << endl
<< "Downtown Line: " << dtline.getlines() << endl
<< "Other Lines: " << orline.getlines() << endl;
QPieSlice *slice = series->slices().at(1);
slice->setExploded();
slice->setLabelVisible();
slice->setPen(QPen(Qt::darkGreen, 2));
slice->setBrush(Qt::green);
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("MRT Disruption Pie Chart");
//chart->legend()->hide();
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
//Display chart
ui.verticalLayout_11->addWidget(chartView);
//Return back to line colour chart display page
ui.stackedWidget->setCurrentIndex(5);
//Remove previous chart to prevent duplicate
ui.verticalLayout_11->removeWidget(chartView);
}
После отладки я должен получить в качестве вывода следующие значения:
/*
><Respective Train Breakdown Count>
>North-South Line: 190
>East-West Line: 203
>North-East Line: 50
>Circle Line: 66
>Downtown Line: 12
>Other Lines: 53
>Total Count: 574
*/
Однако вместо этого я получаю:
/*
><Respective Train Breakdown Count 2>
>North-South Line: 2
>East-West Line: 1514685496
>North-East Line: 1953534480
>Circle Line: 1514685496
>Downtown Line: 1953534480
>Other Lines: -1479339952
*/
Любойключи?Некоторая помощь будет оценена (: