使用 getter 函数时,C++ 无法检索正确的值



我正在尝试使用getter从"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 
*/

有什么线索吗?一些帮助将不胜感激(:

您的训练线变量(如nsline nsline)在readJsn函数声明,因此它们超出范围并在函数结束时消失。
不使用您在MainGUI中声明的具有相同名称的副本,并且具有随机内容。

您需要在外部声明它们(在MainGUI中,就像您所做的那样),并在readJsn函数中传递它们以进行填充。仅具有同名变量不会以任何方式关联它们。

相关内容

  • 没有找到相关文章

最新更新