我可以成功地保存我创建的json文件,并且我知道它保存在哪个目录中,但我希望将保存它的文件路径显示为文本显示。我被困在使用哪个Qt类和函数上了。下面是我的save_json()函数:
void EditWindow::save_json()
{
// update Plot vector
for(size_t i = 0; i < plots.size(); i++) {
//update json object
if(plots[i]->save_changes()) {
// reload_nodes(i);
}
}
/* Update json file
*
*/
cout<<"Json filename" <<jsonfile<<endl;
QFile saveFile(QString::fromStdString(jsonfile));
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open save file.");
}
QJsonArray plotsArray;
for(size_t i = 0; i < plots.size(); i++){
plotsArray.append(plots[i]->get_json_val());
}
QJsonDocument saveDoc(plotsArray);
saveFile.write(saveDoc.toJson());
setup_plot_list();
cout<<"JSON FILE SAVED"<<endl;
}
发布于 2018-07-26 08:15:57
假设_std::string jsonfile和QLabel *text是EditWindow的成员,使用QFileInfo执行以下操作:
...
jsonfile.assign(...); // this is where you set the value of jsonfile
QFileInfo fi(QString::fromStdString(jsonfile));
text->setText(fi.path());
...
这应该可以完成工作,但是您可以试验不同的QFileInfo方法,以便显示不同的信息。
https://stackoverflow.com/questions/51528717
复制相似问题