﻿{"id":1436,"date":"2024-05-31T08:22:48","date_gmt":"2024-05-31T00:22:48","guid":{"rendered":"https:\/\/www.tansoo.cn\/?p=1436"},"modified":"2024-05-31T08:22:48","modified_gmt":"2024-05-31T00:22:48","slug":"phpbb-83","status":"publish","type":"post","link":"https:\/\/www.tansoo.cn\/?p=1436","title":{"rendered":"QString\u5b57\u7b26\u4e32\u8f6cchar*\u7b49\u51fd\u6570"},"content":{"rendered":"<p>\u5728\u8fdb\u884c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u65f6\uff0c\u4e00\u4e2a\u5c40\u90e8QString\u5bf9\u8c61\uff0c\u7ecf\u8fc7\u8f6c\u6362\u6210char*\u540e\uff0c\u5f53\u505a\u53c2\u6570\u4f20\u5165\u7ebf\u7a0b\u51fd\u6570\uff0c\u7ed3\u679c\u8f93\u5165\u4e00\u76f4\u4e3a\u7a7a\u3002<\/p>\n<p><span style=\"color:#FF0000\"><strong class=\"text-strong\">qPrintable() \u548c strdup()<\/strong><\/span><br \/>\nqPrintable() \u548c strdup() \u662f Qt \u4e2d\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u51fd\u6570\u3002<br \/>\nqPrintable() \u51fd\u6570\u7528\u4e8e\u5c06 QString \u7c7b\u578b\u8f6c\u6362\u6210 char* \u7c7b\u578b\uff0c\u7528\u4e8e C++ \u4e2d\u4f7f\u7528\u7684\u5b57\u7b26\u4e32\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"cpp\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QString str <span style=\"color: #000080;\">=<\/span> <span style=\"color: #FF0000;\">&quot;Hello World&quot;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">qDebug<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #000080;\">&lt;&lt;<\/span> qPrintable<span style=\"color: #008000;\">&#40;<\/span>str<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span> &nbsp; &nbsp; <span style=\"color: #666666;\">\/\/ \u8f93\u51fa\uff1aHello World<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u8be5\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u4e34\u65f6\u6307\u9488\uff0c\u4ec5\u5728\u5f53\u524d\u8bed\u53e5\u5757\u5185\u6709\u6548\u3002<\/p>\n<p><span style=\"color:#FF0000\"><strong class=\"text-strong\">strdup() \u51fd\u6570<\/strong><\/span>\u7528\u4e8e\u5c06\u4e00\u4e2a C \u98ce\u683c\u7684\u5b57\u7b26\u4e32\u62f7\u8d1d\u5230\u4e00\u4e2a\u65b0\u7684\u5185\u5b58\u5730\u5740\u4e2d\uff0c\u5e76\u8fd4\u56de\u8be5\u5730\u5740\u7684\u6307\u9488\u3002\u4f8b\u5982\uff1a<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"cpp\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #0000ff;\">const<\/span> <span style=\"color: #0000ff;\">char<\/span><span style=\"color: #000040;\">*<\/span> str1 <span style=\"color: #000080;\">=<\/span> <span style=\"color: #FF0000;\">&quot;Hello World&quot;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #0000ff;\">char<\/span><span style=\"color: #000040;\">*<\/span> str2 <span style=\"color: #000080;\">=<\/span> strdup<span style=\"color: #008000;\">&#40;<\/span>str1<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #666666;\">\/\/ \u4f7f\u7528 str2<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #0000dd;\">free<\/span><span style=\"color: #008000;\">&#40;<\/span>str2<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span> &nbsp; <span style=\"color: #666666;\">\/\/ \u91ca\u653e str2 \u6307\u5411\u7684\u5185\u5b58<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u8be5\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65b0\u7684\u5185\u5b58\u5730\u5740\uff0c\u9700\u8981\u624b\u52a8\u91ca\u653e\u3002<\/p>\n<p>\u5728\u591a\u7ebf\u7a0b\u4e2d\uff0c\u7531\u4e8e\u591a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u9700\u8981\u6ce8\u610f\u4f7f\u7528\u4e34\u65f6\u6307\u9488\u7684\u95ee\u9898\u3002\u5efa\u8bae\u5728\u591a\u7ebf\u7a0b\u4e2d\u4f7f\u7528 constData() \u51fd\u6570\u83b7\u53d6\u5230\u5b57\u7b26\u4e32\u7684\u5e38\u91cf\u6307\u9488\uff0c\u7136\u540e\u4f7f\u7528 strdup() \u51fd\u6570\u4e3a\u5176\u5206\u914d\u5185\u5b58\uff0c\u4f7f\u7528\u5b8c\u540e\u518d\u4f7f\u7528 free() \u51fd\u6570\u91ca\u653e\u5185\u5b58\uff0c\u4ee5\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<p><span style=\"color:#0000BF\"><span style=\"font-size:150%;line-height:116%\">\u5176\u4ed6\u8f6c\u6362\u51fd\u6570<\/span><\/span><br \/>\n\u9664\u4e86 qPrintable() \u548c strdup()\uff0c\u5728 Qt \u4e2d\u8fd8\u6709\u8bb8\u591a\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u51fd\u6570\u3002<br \/>\n\u4ee5\u4e0b\u662f\u5176\u4e2d\u7684\u4e00\u4e9b\uff1a<\/p>\n<p><strong class=\"text-strong\"><span style=\"color:#FF0000\">toStdString()\uff1a<\/span><\/strong>\u5c06 QString \u7c7b\u578b\u8f6c\u6362\u4e3a std::string \u7c7b\u578b\u3002<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"cpp\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QString str <span style=\"color: #000080;\">=<\/span> <span style=\"color: #FF0000;\">&quot;Hello World&quot;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">std<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">string<\/span> stdStr <span style=\"color: #000080;\">=<\/span> str.<span style=\"color: #007788;\">toStdString<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><strong class=\"text-strong\"><span style=\"color:#FF0000\">fromStdString()\uff1a<\/span><\/strong>\u5c06 std::string \u7c7b\u578b\u8f6c\u6362\u4e3a QString \u7c7b\u578b\u3002<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"cpp\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">std<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">string<\/span> stdStr <span style=\"color: #000080;\">=<\/span> <span style=\"color: #FF0000;\">&quot;Hello World&quot;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QString str <span style=\"color: #000080;\">=<\/span> QString<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">fromStdString<\/span><span style=\"color: #008000;\">&#40;<\/span>stdStr<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><strong class=\"text-strong\"><span style=\"color:#FF0000\">toLatin1()\uff1a<\/span><\/strong>\u5c06 QString \u7c7b\u578b\u8f6c\u6362\u4e3a QByteArray \u7c7b\u578b\u7684 Latin-1 \u7f16\u7801\u3002<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"cpp\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QString str <span style=\"color: #000080;\">=<\/span> <span style=\"color: #FF0000;\">&quot;Hello World&quot;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QByteArray latin1Array <span style=\"color: #000080;\">=<\/span> str.<span style=\"color: #007788;\">toLatin1<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><strong class=\"text-strong\"><span style=\"color:#FF0000\">toUtf8()\uff1a<\/span><\/strong>\u5c06 QString \u7c7b\u578b\u8f6c\u6362\u4e3a QByteArray \u7c7b\u578b\u7684 UTF-8 \u7f16\u7801\u3002<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"cpp\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QString str <span style=\"color: #000080;\">=<\/span> <span style=\"color: #FF0000;\">&quot;Hello World&quot;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QByteArray utf8Array <span style=\"color: #000080;\">=<\/span> str.<span style=\"color: #007788;\">toUtf8<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><strong class=\"text-strong\"><span style=\"color:#FF0000\">fromLatin1()\uff1a<\/span><\/strong>\u5c06 QByteArray \u7c7b\u578b\u7684 Latin-1 \u7f16\u7801\u8f6c\u6362\u4e3a QString \u7c7b\u578b\u3002<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"cpp\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QByteArray latin1Array <span style=\"color: #000080;\">=<\/span> <span style=\"color: #FF0000;\">&quot;Hello World&quot;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QString str <span style=\"color: #000080;\">=<\/span> QString<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">fromLatin1<\/span><span style=\"color: #008000;\">&#40;<\/span>latin1Array<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><strong class=\"text-strong\"><span style=\"color:#FF0000\">fromUtf8()\uff1a<\/span><\/strong>\u5c06 QByteArray \u7c7b\u578b\u7684 UTF-8 \u7f16\u7801\u8f6c\u6362\u4e3a QString \u7c7b\u578b\u3002<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"cpp\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QByteArray utf8Array <span style=\"color: #000080;\">=<\/span> <span style=\"color: #FF0000;\">&quot;Hello World&quot;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">QString str <span style=\"color: #000080;\">=<\/span> QString<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">fromUtf8<\/span><span style=\"color: #008000;\">&#40;<\/span>utf8Array<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u591a\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u65f6\uff0c\u4e5f\u9700\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n<p><small>\u539f\u6587\u4f5c\u8005: BG6RSH | <a href=\"https:\/\/bbs.tansoo.cn\/viewtopic.php?t=83\" target=\"_blank\" rel=\"noopener\">\u67e5\u770b\u539f\u5e16<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u8fdb\u884c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u65f6\uff0c\u4e00\u4e2a\u5c40\u90e8QString\u5bf9\u8c61\uff0c\u7ecf\u8fc7\u8f6c\u6362\u6210char*\u540e\uff0c\u5f53\u505a\u53c2\u6570 &hellip; <a href=\"https:\/\/www.tansoo.cn\/?p=1436\">\u7ee7\u7eed\u9605\u8bfb <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":[105],"tags":[],"class_list":["post-1436","post","type-post","status-publish","format-standard","hentry","category-105"],"_links":{"self":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1436"}],"version-history":[{"count":0,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1436\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}