From 4d1ebeccfa52ff1b1799e8dc5ae8248733989334 Mon Sep 17 00:00:00 2001 From: Eivind Kvedalen Date: Thu, 13 Aug 2015 23:35:37 +0200 Subject: [PATCH] Base: Added toStdString and fromStdString, to convert std::string to/from QString, assuming std::string is utf-8 encoded. --- src/Base/Tools.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Base/Tools.h b/src/Base/Tools.h index cb2f551c1234..c0b98cd62f51 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