diff --git a/src/Base/Tools.h b/src/Base/Tools.h index cb2f551c1..c0b98cd62 100644 --- a/src/Base/Tools.h +++ b/src/Base/Tools.h @@ -30,6 +30,7 @@ #include #include #include +#include namespace Base { @@ -154,8 +155,23 @@ struct BaseExport Tools static std::wstring widen(const std::string& str); static std::string narrow(const std::wstring& str); static std::string escapedUnicodeFromUtf8(const char *s); + + /** + * @brief toStdString Convert a QString into a UTF-8 encoded std::string. + * @param s String to convert. + * @return A std::string encoded as UTF-8. + */ + static inline std::string toStdString(const QString& s) { QByteArray tmp = s.toUtf8(); return std::string(tmp.constData(), tmp.size()); } + + /** + * @brief fromStdString Convert a std::string encoded as UTF-8 into a QString. + * @param s std::string, expected to be UTF-8 encoded. + * @return String represented as a QString. + */ + static inline QString fromStdString(const std::string & s) { return QString::fromUtf8(s.c_str(), s.size()); } }; + } // namespace Base #endif // BASE_TOOLS_H