4

我创建了一个包含作为字符串的 yamlmap,并将其保存到文本文件中。加载相同的文本文件时,包含字符串的键由 yaml 自动排序。现在我想加载相同的字符串名称按照我保存的方式订购。为了解决这个问题,我必须在函数 saveSettings() 和 loadSettings 中做些什么。谢谢

std::vector<SettingsPair*> settingsvector;std::list<std::string> settings_list;
class SettingsPair
{
public:
    /**
     * @brief standard constructor
     */
    SettingsPair();
    SettingsPair(QLineEdit *le, QLabel *la);
    ~SettingsPair();
    QLineEdit* line_edit;
    QLabel* label;

};


   void task1::saveSettings()
{
    QString filename = QFileDialog::getSaveFileName(this,
                                                    tr("save YAML File "),"C:/Users/marthala/Desktop/ws/settingstask","ALL files(*.*);;YAML File(*.yaml)");
    std::ofstream ofstr;
    std::string file= filename.toStdString();
    YAML::Emitter out;
    ofstr.open(file.c_str());
    out << YAML::BeginMap;
    for(std::vector<SettingsPair*>::iterator list_iter = settingsvector.begin();
        list_iter != settingsvector.end(); list_iter++)
    {
        QString key_lineedit = (*list_iter)->line_edit->text();
        QString key_label = (*list_iter)->label->text();
        out << YAML::Key << key_label.toStdString();
        out << YAML::Value <<key_lineedit.toStdString();

    }
    ofstr<<out.c_str();
    out << YAML::EndMap;

    std::cout<<"here is the output while saving the file:\n"<<out.c_str() ;
    ofstr.close();

}

   void task1::loadSettings()
{
    QString filename = QFileDialog::getOpenFileName(this,
                                                    tr("Open File"),"C:/Users/marthala/Desktop/ws/settingstask","ALL files(*.*);;YAML File(*.yaml)");
    //getOpenFileName will retuns the file path of the file selected

    if(filename.isEmpty())
    {
        return;
    }
    else
    {
        try
        {

            std::ifstream fin(filename.toStdString().c_str());
            YAML::Parser parser(fin);
            YAML::Node doc;
            while(parser.GetNextDocument(doc))
            {
                std::vector<SettingsPair*>::iterator list_iter = settingsvector.begin();
                if(list_iter != settingsvector.end())
                {
                    std::string key_, value_;
                    for(YAML::Iterator it=doc.begin();it!=doc.end();++it)
                    {

                        it.first() >> key_;
                        it.second() >> value_;

                        QString str1 = QString::fromUtf8(key_.c_str());
                        QString str2 = QString::fromUtf8(value_.c_str());
                        (*list_iter)->label->setText(str1);
                        (*list_iter)->line_edit->setText(str2);
                        list_iter++;

                        std::cout<<"here is the output while loading the file:\n"<< key_<<":\n"<<value_;
                    }
                }
            }

        }
        catch(YAML::ParserException &e)
        {
            std::cout<<"YAML Exception caught"<<e.what()<<std::endl;
        }
    }
}
4

2 回答 2

0

此功能目前正在考虑中。请参阅 项目页面上的问题报告以获取更多详细信息。

于 2014-05-08T13:07:09.603 回答
0

您需要 yaml-cpp 0.6 或更高版本

https://github.com/jbeder/yaml-cpp/commit/f0b15cd6a0d61f5b2dbc317263c02f91a7e3692c

于 2021-07-15T18:26:59.297 回答