When wrapping a Qt UI around back-end code using boost::filesystem
one frequently needs to convert boost::filesystem::path
to QString
and vice versa.
What is the best way way to do these conversions that:
QString
s containing regular slashes on all platforms, as is Qt's policy.This is what I'm currently using, but suggestions for improvements are very much welcome.
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
}