{"id":235,"date":"2011-08-10T10:41:36","date_gmt":"2011-08-10T09:41:36","guid":{"rendered":"http:\/\/blog.andresgomez.org\/?p=235"},"modified":"2012-07-26T02:03:10","modified_gmt":"2012-07-26T01:03:10","slug":"qurl-misusage-appendix-avoid-automatic-cast-from-qstring","status":"publish","type":"post","link":"https:\/\/blog.andresgomez.org\/es\/2011\/08\/10\/qurl-misusage-appendix-avoid-automatic-cast-from-qstring\/","title":{"rendered":"QUrl (mis)usage: appendix, avoid automatic cast from QString"},"content":{"rendered":"<p>As I was introducing in the former entry <a href=\"http:\/\/blog.andresgomez.org\/2011\/07\/19\/qurl-misusage\/\" title=\"QUrl (mis)usage\" target=\"_blank\">QUrl (mis)usage<\/a>, the direct creation of a QUrl from a QString should be avoided in any software that is not trying to <em>smartly<\/em> guess what a user input should lead to.<\/p>\n<p>So, going directly to the <strong>ham<\/strong>, to avoid mistakes due to automatic conversions from QString to QUrl, I encourage the usage of the <a href=\"http:\/\/doc.qt.nokia.com\/4.7\/qurl.html#QT_NO_URL_CAST_FROM_STRING\" title=\"QT_NO_URL_CAST_FROM_STRING\" target=\"_blank\">QT_NO_URL_CAST_FROM_STRING<\/a> macro. The only thing you have to do is adding a line to your <em>qmake<\/em> project file like this:<\/p>\n<p style=\"font-size: x-small;\"><code># Avoid automatic casts from QString to QUrl. Dangerous!!!<br \/>\nDEFINES += QT_NO_URL_CAST_FROM_STRING<\/code><\/p>\n<p>Or add it directly to the compilation line, like this<\/p>\n<p style=\"font-size: x-small;\"><code>g++ ... -DQT_NO_URL_CAST_FROM_STRING ... <\/code><\/p>\n<p>As I was pointing in my previous post, the usage of <a href=\"http:\/\/doc.qt.nokia.com\/4.7\/qurl.html#fromLocalFile\" title=\"QUrl::fromLocalFile API documentation\" target=\"_blank\">QUrl::fromLocalFile(QString)<\/a> and <a href=\"http:\/\/doc.qt.nokia.com\/4.7\/qurl.html#fromEncoded-2\" title=\"QUrl::fromEncoded API documentation\" target=\"_blank\">QUrl::fromEncoded(QByteArray, QUrl::StrictMode)<\/a> is recommended when dealing with QString and QUrl, but committing mistakes is a human condition so it is pretty easy to end passing a QString as a parameter to some API expecting a QUrl, or assigning a QString to a QUrl with the \u00ab=\u00bb operator through the C++ automatic cast mechanism which is implemented in the QUrl class. That&#8217;s why <strong>forbidding<\/strong> these <strong>automatic casts<\/strong> in our code is of such importantance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I was introducing in the former entry QUrl (mis)usage, the direct creation of a QUrl from a QString should be avoided in any software that is not trying to smartly guess what a user input should lead to. So, &hellip; <a href=\"https:\/\/blog.andresgomez.org\/es\/2011\/08\/10\/qurl-misusage-appendix-avoid-automatic-cast-from-qstring\/\">Sigue leyendo <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6,7,9,19,11,20,12,21],"tags":[],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-english","category-free-software","category-general","category-igaliacom","category-linkedin","category-meego","category-mobile","category-planetigaliacom","category-qt"],"_links":{"self":[{"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/posts\/235"}],"collection":[{"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":13,"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":438,"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/posts\/235\/revisions\/438"}],"wp:attachment":[{"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.andresgomez.org\/es\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}