【问题标题】:How to convert between `boost::filesystem::path` and `QString`?如何在`boost::filesystem::path`和`QString`之间转换?
【发布时间】:2018-12-12 13:36:18
【问题描述】:

当使用boost::filesystem 围绕后端代码包装Qt UI 时,经常需要将boost::filesystem::path 转换为QString,反之亦然。

进行这些转换的最佳方式是:

  1. 是跨平台的
  2. 无损保留编码
  3. 按照 Qt 的政策,在所有平台上生成包含正斜杠的 QStrings。
  4. 高效且避免不必要的复制

【问题讨论】:

    标签: qt qstring boost-filesystem


    【解决方案1】:

    这是我目前正在使用的,但非常欢迎提出改进建议。

    boost::filesystem::path PathFromQString(const QString & filePath)
    {
    #ifdef _WIN32
        auto * wptr = reinterpret_cast<const wchar_t*>(filePath.utf16());
        return boost::filesystem::path(wptr, wptr + filePath.size());
    #else
        return boost::filesystem::path(filePath.toStdString());
    #endif
    }
    
    QString QStringFromPath(const boost::filesystem::path & filePath)
    {
    #ifdef _WIN32
        return QString::fromStdWString(filePath.generic_wstring());
    #else
        return QString::fromStdString(filePath.native());
    #endif
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 2012-07-06
      • 2016-06-02
      • 2011-04-23
      • 2018-02-18
      • 1970-01-01
      • 2011-01-24
      • 2011-11-07
      • 1970-01-01
      相关资源
      最近更新 更多