Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt with yaml-cpp in qrc file
Forum Updated to NodeBB v4.3 + New Features

Qt with yaml-cpp in qrc file

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nan Feng
    wrote on last edited by
    #1

    Hi, I am trying to use Qt and yaml-cpp library to read and write .yaml, if it is an absolute path (C:/test.yaml), everything is normal. If it is a .yaml file in qrc, the program crashes directly and cannot be started. It seems that yaml-cpp cannot recognize the .yaml file in this qrc. Is there any solution?

    KroMignonK 1 Reply Last reply
    0
    • N Nan Feng

      Hi, I am trying to use Qt and yaml-cpp library to read and write .yaml, if it is an absolute path (C:/test.yaml), everything is normal. If it is a .yaml file in qrc, the program crashes directly and cannot be started. It seems that yaml-cpp cannot recognize the .yaml file in this qrc. Is there any solution?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @Nan-Feng said in Qt with yaml-cpp in qrc file:

      It seems that yaml-cpp cannot recognize the .yaml file in this qrc. Is there any solution?

      If you want to got help, you have to show the code you are using to read the yaml file from qrc.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nan Feng
        wrote on last edited by
        #3

        code:

        YAML::Node config = YAML::LoadFile(":/assets/test.yaml");

        std::string value = config["name"].asstd::string();

        YAML::Node config = YAML::LoadFile("C:/test.yaml");

        std::string value = config["name"].asstd::string();

        With the above wording, the program crashes directly.
        In the following way of writing, the value corresponding to the name key can be read normally

        KroMignonK 1 Reply Last reply
        0
        • N Nan Feng

          code:

          YAML::Node config = YAML::LoadFile(":/assets/test.yaml");

          std::string value = config["name"].asstd::string();

          YAML::Node config = YAML::LoadFile("C:/test.yaml");

          std::string value = config["name"].asstd::string();

          With the above wording, the program crashes directly.
          In the following way of writing, the value corresponding to the name key can be read normally

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @Nan-Feng said in Qt with yaml-cpp in qrc file:

          YAML::Node config = YAML::LoadFile(":/assets/test.yaml");

          I don't think YAML supports RC files.
          You have to read the the yaml file into a QString and then passe it to YAML.

          Something like:

          QFile file(":/assets/test.yaml");
          if(file.open(QFile::ReadOnly | QFile::Text))
          {
              QTextStream in(&file);
              QString myText = in.readAll();
              YAML::Node config(myText.toStdString());
          }
          
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          2
          • KH-219DesignK Offline
            KH-219DesignK Offline
            KH-219Design
            wrote on last edited by
            #5

            I agree with @KroMignon 's approach.

            Another alternative (in case it becomes appealing for any other reason) is to use QTemporaryFile:

              // note the ":" at the front of the path
              std::string path_inside_qrc = ":/data/sim_rpc_replies_immediate_scores.pbtxt";
              std::unique_ptr<QTemporaryFile> temporary_file;
            
              temporary_file.reset(
                    QTemporaryFile::createNativeFile(QString::fromStdString(path_inside_qrc))
              );
              std::string file_name = temporary_file->fileName().toUtf8().constData();
              
              // TODO: add code to use the temporary file.
             
              // Delete the temp file after using it
              temporary_file.reset();
            

            (This is "self plagiarism" from this prior discussion thread.)

            www.219design.com
            Software | Electrical | Mechanical | Product Design

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved