{"id":2378,"date":"2015-06-02T11:54:57","date_gmt":"2015-06-02T10:54:57","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2378"},"modified":"2015-06-02T11:54:57","modified_gmt":"2015-06-02T10:54:57","slug":"qstring-to-stdstring-bug-of-the-day-8","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2378","title":{"rendered":"QString to std::string (Bug of the day 8)"},"content":{"rendered":"<pre><code>std::string class::func() const noexcept {\r\n    return name.toLatin1().constData();\r\n}\r\n<\/code><\/pre>\n<p>3 convertions. From QString to QByteArray to const char* to std::string. Lets see if it works. From Qt Docu<\/p>\n<blockquote><p>const char * QByteArray::?constData() const<br \/>\nReturns a pointer to the data stored in the byte array. The pointer can be used to access the bytes that compose the array. <strong>The data is '\\0'-terminated<\/strong> unless the QByteArray object was created from raw data.\n<\/p><\/blockquote>\n<blockquote><p>basic_string( const CharT* s,  const Allocator& alloc = Allocator() )<br \/>\n Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by s. The length of the string is determined by the first null character. The behavior is undefined if s does not point at an array of at least Traits::length(s)+1 elements of CharT.\n<\/p><\/blockquote>\n<p>Okay this should work, but can fail.<\/p>\n<p>Using Qt function to convert QString to std::string is the better solution: <\/p>\n<pre><code>std::string QString::toStdString() const<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>std::string class::func() const noexcept { return name.toLatin1().constData(); } 3 convertions. From QString to QByteArray to const char* to std::string. Lets see if it works. From Qt Docu const char * QByteArray::?constData() const Returns a pointer to the data stored in the byte array. The pointer can be used to access the bytes that compose the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17,12],"class_list":["post-2378","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp","tag-qt"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2378"}],"version-history":[{"count":1,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2378\/revisions"}],"predecessor-version":[{"id":2379,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2378\/revisions\/2379"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2378"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}