Конвертировать QT Resource в :: std :: istream & - PullRequest
0 голосов
/ 25 февраля 2019

Я пытаюсь преобразовать QResource XML в istream.Подпись третьей стороны, в которую входит XML, показана ниже.

  ::std::unique_ptr< ::NO::Peoplefile >
  peoplefile (::std::istream& is,
         ::xml_schema::Flags f = 0,
         const ::xml_schema::Properties& p = ::xml_schema::Properties ());

Но как мне преобразовать QResource в istream?

пытался использовать приведенное ниже, но не смог преобразовать егодо ::std::istream&.Есть идеи?

ifstream& QFileToifstream(QFile & file) {
    Q_ASSERT(file.isReadable());
    return ifstream(::_fdopen(file.handle(), "r")); //error: non-const lvalue reference to type 'basic_ifstream<...>' cannot bind to a temporary of type 'basic_ifstream<...>'
}

1 Ответ

0 голосов
/ 25 февраля 2019

Поскольку ресурс, о котором идет речь, - это XML, я предполагаю, что он невелик и легко может быть полностью записан в память.В таком случае вы можете просто использовать std::istringstream ...

/*
 * Attempt to open the bound resource `:/some-resource.xml'.
 */
if (QFile f(":/some-resource.xml"); f.open(QIODevice::ReadOnly)) {

  /*
   * Read its contents into an std::string which is used to
   * initialize an std::istringstream.
   */
  std::istringstream is(f.readAll().toStdString());

  /*
   * Pass the istringstream as the first parameter to peoplefile.
   */
  auto result = peoplefile(is, 0/* flags */, ::xml_schema::Properties());
}
...