将QT资源转换为:: std :: istream&

问题描述 投票:0回答:1

我正在尝试将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<...>'
}
qt ifstream qfile
1个回答
0
投票

由于所讨论的资源是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());
}
© www.soinside.com 2019 - 2024. All rights reserved.