{"id":26,"date":"2021-08-04T11:10:09","date_gmt":"2021-08-04T03:10:09","guid":{"rendered":"http:\/\/www.wu-xk.cn\/?p=26"},"modified":"2025-03-24T23:13:17","modified_gmt":"2025-03-24T15:13:17","slug":"qt5%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81","status":"publish","type":"post","link":"https:\/\/itcat.me\/?p=26","title":{"rendered":"QT5\u4e2d\u6587\u4e71\u7801"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Qt\u7684\u4e2d\u6587\u4e71\u7801\uff0c\u7f51\u4e0a\u4e00\u641c\u90fd\u662f\u8fd9\u4e2a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers cbp-highlight-hover\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(1 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(16, 41, 67, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#fff;font-style:italic;color:#24292e\"><span style=\"border-bottom:1px solid rgba(16, 41, 67, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"QTextCodec\u00a0*codec\u00a0=\u00a0QTextCodec::codecForName(&quot;GB2312&quot;);\n\nQTextCodec::setCodecForLocale(codec);\n\nQTextCodec::setCodecForCStrings(codec);\n\nQTextCodec::setCodecForTr(codec);\" style=\"color:#24292e;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">QTextCodec\u00a0<\/span><span style=\"color: #D73A49\">*<\/span><span style=\"color: #24292E\">codec\u00a0<\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\">\u00a0<\/span><span style=\"color: #6F42C1\">QTextCodec<\/span><span style=\"color: #24292E\">::<\/span><span style=\"color: #6F42C1\">codecForName<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;GB2312&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6F42C1\">QTextCodec<\/span><span style=\"color: #24292E\">::<\/span><span style=\"color: #6F42C1\">setCodecForLocale<\/span><span style=\"color: #24292E\">(codec);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6F42C1\">QTextCodec<\/span><span style=\"color: #24292E\">::<\/span><span style=\"color: #6F42C1\">setCodecForCStrings<\/span><span style=\"color: #24292E\">(codec);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6F42C1\">QTextCodec<\/span><span style=\"color: #24292E\">::<\/span><span style=\"color: #6F42C1\">setCodecForTr<\/span><span style=\"color: #24292E\">(codec);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u8fd9\u4e2a\u662fQt4\u7684\uff0cQt5\u4e2d\u5df2\u7ecf\u4e0d\u80fd\u7528\u4e86\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers cbp-highlight-hover\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(1 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(16, 41, 67, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#fff;font-style:italic;color:#24292e\"><span style=\"border-bottom:1px solid rgba(16, 41, 67, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"QString s1 = &quot;\u6c49\u8bed&quot;;\nQString s2(&quot;\u6f22\u8a9e&quot;);\nQString s3 = tr(&quot;\u4e2d\u6587&quot;)\nQString s4 = QStringLiteral(&quot;\u4e2d\u6587&quot;);\/\/\u53ea\u8981\u5b57\u7b26\u4e32\u4e0d\u9700\u8981\u7ffb\u8bd1\uff0c\u8bf7\u5173\u6ce8\u8fd9\u4e2a\nQString s5 = QString::fromWCharArray(L&quot;\u4e2d\u6587&quot;);\nQString s6 = u8&quot;\u4e2d\u6587&quot;;\/\/C++11\nQString s7 = tr(u8&quot;\u4e2d\u6587&quot;)\" style=\"color:#24292e;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">QString s1 <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">&quot;\u6c49\u8bed&quot;<\/span><span style=\"color: #24292E\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6F42C1\">QString<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">s2<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;\u6f22\u8a9e&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">QString s3 <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">tr<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;\u4e2d\u6587&quot;<\/span><span style=\"color: #24292E\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">QString s4 <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">QStringLiteral<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;\u4e2d\u6587&quot;<\/span><span style=\"color: #24292E\">);<\/span><span style=\"color: #6A737D\">\/\/\u53ea\u8981\u5b57\u7b26\u4e32\u4e0d\u9700\u8981\u7ffb\u8bd1\uff0c\u8bf7\u5173\u6ce8\u8fd9\u4e2a<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">QString s5 <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">QString<\/span><span style=\"color: #24292E\">::<\/span><span style=\"color: #6F42C1\">fromWCharArray<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">L&quot;\u4e2d\u6587&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">QString s6 <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">u8&quot;\u4e2d\u6587&quot;<\/span><span style=\"color: #24292E\">;<\/span><span style=\"color: #6A737D\">\/\/C++11<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">QString s7 <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">tr<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">u8&quot;\u4e2d\u6587&quot;<\/span><span style=\"color: #24292E\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u6709\u8fd9\u4e9b\u5728Qt5\u9ed8\u8ba4\u90fd\u4f1a\u6b63\u5e38\u5de5\u4f5c\uff0c\u552f\u4e00\u8981\u6c42\u5c31\u662f\uff1a\u786e\u4fdd\u4f60\u7684C++\u7684\u6267\u884c\u5b57\u7b26\u96c6(the execution character set)\u662fUTF-8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5efa\u8bae\u7528QStringLiteral\u3002<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_26\" class=\"pvc_stats all  \" data-element-id=\"26\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/itcat.me\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Qt\u7684\u4e2d\u6587\u4e71\u7801\uff0c\u7f51\u4e0a\u4e00\u641c\u90fd\u662f\u8fd9\u4e2a\uff1a \u4f46\u8fd9\u4e2a\u662fQt4\u7684\uff0cQt5\u4e2d\u5df2\u7ecf\u4e0d\u80fd\u7528\u4e86\u3002 \u6240\u6709\u8fd9\u4e9b\u5728Qt5\u9ed8\u8ba4\u90fd . . . <a class=\"readmore-link\" href=\"https:\/\/itcat.me\/?p=26\">Read more<\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_26\" class=\"pvc_stats all  \" data-element-id=\"26\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/itcat.me\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/itcat.me\/index.php?rest_route=\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itcat.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itcat.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itcat.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itcat.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26"}],"version-history":[{"count":3,"href":"https:\/\/itcat.me\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/itcat.me\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/itcat.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itcat.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itcat.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}