{"id":266297,"date":"2023-03-05T14:06:34","date_gmt":"2023-03-05T06:06:34","guid":{"rendered":"https:\/\/lrxjmw.cn\/qzdicztgtydj.html"},"modified":"2023-03-05T14:06:34","modified_gmt":"2023-03-05T06:06:34","slug":"qzdicztgtydj","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/qzdicztgtydj.html","title":{"rendered":"QT\u4e2d\u7684IO\u64cd\u4f5c\u901a\u8fc7\u7edf\u4e00\u7684\u63a5\u53e3\u7b80\u5316\u4e86\u6587\u4ef6\u4e0e\u5916\u90e8\u8bbe\u5907\u7684\u64cd\u4f5c\u65b9\u5f0f"},"content":{"rendered":"

\u7b80\u4ecb<\/p>\n

QT\u4e2d\u7684IO\u64cd\u4f5c\u901a\u8fc7\u7edf\u4e00\u7684\u63d2\u53e3\u7b80\u5316\u4e86\u6587\u4ef6\u4e0e\u5916\u90e8\u8bbe\u5907\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0cQT\u4e2d\u6587\u4ef6\u88ab\u5f53\u6210\u4e00\u79cd\u7279\u6b8a\u7684\u5916\u90e8\u8bbe\u5907\uff0c\u6587\u4ef6\u64cd\u4f5c\u4e0e\u5916\u90e8\u8bbe\u5907\u64cd\u4f5c\u76f8\u540c\u3002IO\u64cd\u4f5c\u7684\u672c\u8d28\u662f\u8fde\u7eed\u50a8\u5b58\u7a7a\u95f4\u7684\u6570\u636e\u8bfb\u5199\u3002<\/p>\n

1.IO\u8bbe\u5907\u7684\u7c7b\u578b<\/p>\n

\u6b21\u5e8f\u5b58\u53d6\u8bbe\u5907\uff1a\u53ea\u80fd\u4ece\u5934\u5f00\u59cb\u6b21\u5e8f\u8bfb\u5199\u6570\u636e\uff0c\u4e0d\u80fd\u6307\u5b9a\u6570\u636e\u7684\u8bfb\u5199\u4f4d\u7f6e<\/p>\n

\u968f\u673a\u5b58\u53d6\u8bbe\u5907\uff1a\u53ef\u4ee5\u5b9a\u4f4d\u5230\u4efb\u610f\u4f4d\u7f6e\u8fdb\u884c\u6570\u636e\u7684\u8bfb\u5199<\/p>\n

linux \u65e5\u5fd7\u5207\u5272\u811a\u672c_linux \u5207\u5272\u6587\u4ef6_linux \u6587\u4ef6\u5207\u5272<\/p>\n

2.QFile<\/p>\n

\u5728Qt\u5e2e\u52a9\u6587\u6863\u4e0a\u9762\uff0c\u5018\u82e5\u76f4\u63a5\u67e5\u8be2QFile\u5e2e\u52a9\u6587\u6863\uff0c\u770b\u4e0d\u5230\u51e0\u4e2a\u5173\u4e8e\u6587\u4ef6\u8bfb\u5199\u7684\u51fd\u6570\uff0c\u7531\u4e8eQt\u5c06\u8bfb\u5199\u64cd\u4f5c\u90fd\u5c01\u88c5\u5728\u6cdb\u578bQIODevice\u4e0a\u9762\uff1a<\/p>\n

QIODevice\u7c7b\u662f\u5bf9\u8f93\u5165\u8f93\u5165\u8bbe\u5907\u7684\u5177\u8c61\u5efa\u6a21\uff0c\u6d89\u53ca\u5230\u8bfb\u5199\u7684\u6587\u4ef6\u7c7bQFile\u3001\u7f51\u7edc\u6536\u53d1QTcpSocket\/QUdpSocket\u3001\u8fdb\u7a0b\u8f93\u5165\u8f93\u51faQProcess\uff0c\u90fd\u662f\u4eceQIODevice\u7c7b\u6d3e\u751f\u7684\u3002QIODevice\u662f\u5341\u5206\u91cd\u8981\u7684\u5b50\u7c7b\uff0c\u4e4b\u540e\u63d0\u5230\u7f51\u8def\u6536\u53d1\u548c\u8fdb\u7a0b\u7c7b\u65f6\u5c31\u4f1a\u518d\u8bb2\uff0c\u672c\u8282\u4e3b\u8981\u5173\u6ce8\u4e0e\u6587\u4ef6\u8bfb\u5199\u76f8\u5173\u7684\u63d2\u53e3\u51fd\u6570\u3002<\/p>\n

QFileDevice\u662f\u5bf9\u6587\u4ef6\u8bbe\u5907\u7684\u5177\u8c61\uff0c\u867d\u7136\u5728Unix\u548cLinux<\/a>\u7cfb\u7edf<\/a>\u4e2d\u6240\u6709\u4e1c\u897f\u90fd\u662f\u6587\u4ef6\u8bbe\u5907\uff0c\u5c31\u8fde\u786c\u4ef6\u8bbe\u5907\u4e5f\u5177\u8c61\u6210\u6587\u4ef6\u8bbe\u5907\uff0c\u4f8b\u5982\/dev\/usb0\u662f\u4ee3\u8868USB\u8bbe\u5907\u7684\u6587\u4ef6\uff0cQFileDevice\u5c31\u662f\u63cf\u8ff0\u6587\u4ef6\u8bbe\u5907\u7684\u7c7b\uff0cQFileDevice\u8fd9\u4e00\u5c42\u6cdb\u578b\u7684\u63d2\u53e3\u51fd\u6570\u6bd4\u8f83\u5c11\uff0c\u53ef\u4ee5\u4e0d\u7528\u7ba1\u7684\u3002<\/p>\n

QFile\u7c7b\u5c31\u662f\u672c\u8282\u7684\u5b66\u4e60\u91cd\u70b9\uff0c\u6b64\u540e\u6e10\u6e10\u8bb2\u89e3\u3002\u4e00\u9f50\u4eceQFileDevice\u6d3e\u751f\u7684\u8fd8\u6709\u4e2aQSaveFile\uff0c\u8fd9\u4e2a\u4fdd\u5b58\u6587\u4ef6\u7c7b\uff0c\u5c31\u4e3a\u4e86\u5b89\u5168\u5730\u4fdd\u5b58\u6587\u4ef6\u800c\u8bbe\u8ba1\u7684\uff0c\u7531\u4e8e\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53ef\u80fd\u6709bug\u9020\u6210\u5d29\u6e83\uff0c\u5047\u5982\u5d29\u6e83\u65f6\u6b63\u5728\u5199\u5165\u6587\u4ef6\uff0c\u8fd9\u4e48\u6587\u4ef6\u88ab\u6539\u5199\u4e86\u4e00\u90e8\u4efd\uff0c\u4f46\u53c8\u6ca1\u66f4\u6539\u5b8c\u5168\uff0c\u4f1a\u9020\u6210\u539f\u59cb\u6587\u4ef6\u7684\u6bc1\u574f\u3002QSaveFile\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u6587\u4ef6\u7684\u4e0d\u5b89\u5168\u8bfb\u5199\uff0c\u9632\u6b62\u51fa\u73b0\u534a\u540a\u5b50\u95ee\u9898\uff0cQSaveFile\u6709\u4e24\u4e2a\u91cd\u8981\u51fd\u6570\uff1acancelWriting()\u51fd\u6570\u53d6\u6d88\u5199\u5165\u64cd\u4f5c\uff0ccommit()\u9012\u4ea4\u6240\u6709\u5199\u5165\u64cd\u4f5c\uff0c\u6653\u5f97QSaveFile\u6709\u8fd9\u4e24\u4e2a\u51fd\u6570\u5c31\u5dee\u4e0d\u591a\u4e86\uff0c\u7531\u4e8e\u4e5f\u6ca1\u5176\u4ed6\u91cd\u8981\u529f\u80fd\u3002QSaveFile\u7c7b\u4e0d\u5355\u72ec\u8bb2\u4e86\uff0c\u7531\u4e8e\u5c31\u90a3\u4e24\u4e2a\u91cd\u8981\u51fd\u6570\u800c\u5df2\u3002\u4e0b\u8fb9\u5f00\u59cb\u5b66\u4e60QFile\u7c7b\u3002<\/p>\n

linux \u65e5\u5fd7\u5207\u5272\u811a\u672c_linux \u6587\u4ef6\u5207\u5272_linux \u5207\u5272\u6587\u4ef6<\/p>\n

\u3010\u9886\u66f4\u591aQT\u5b66\u4e60\u8d44\u6599\uff0c\u70b9\u51fb\u4e0b\u65b9\u94fe\u63a5\u514d\u8d39\u53d1\u653e\u2193\u2193\uff0c\u5148\u7801\u4f4f\u4e0d\u8d70\u5931~\u3011<\/p>\n

\u70b9\u51fb\u2192\u9886\u53d6\u300c\u94fe\u63a5\u300d<\/p>\n

2.1QFile\u529f\u80fd\u51fd\u6570<\/p>\n

\u5728\u591a\u6570\u60c5\u51b5\u4e0b\uff0cQFile\u90fd\u662f\u914d\u5408QTextStream\u548cQDataStream\u4f7f\u7528\uff0c\u5176\u5b9e\u4e5f\u53ef\u4ee5\u4f7f\u7528QFile\u81ea\u5e26\u7684\u8bfb\u5199\u51fd\u6570\u5904\u7406\u6587\u4ef6\u3002QFile\u9664\u4e86\u9002\u5b9c\u4e8e\u666e\u901a\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u5e76\u4e14\u5bf9Qt\u7a0b\u5e8f\u5185\u5d4c\u7684\u8d44\u6e90\u6587\u4ef6\u4e5f\u662f\u901a\u7528\u7684\uff0c\u533a\u522b\u53ea\u662f\u5185\u5d4c\u8d44\u6e90\u6587\u4ef6\u5168\u662f\u53ea\u8bfb\u7684\u3002\u4e0b\u8fb9\u5927\u81f4\u5206\u51e0\u5757\u6765\u4ecb\u7ecdQFile\u7684\u529f\u80fd\u51fd\u6570\uff1a<\/p>\n

\uff081\uff09\u6784\u9020\u51fd\u6570\u548c\u6253\u5f00\u51fd\u6570<\/p>\n

QFile\u4e00\u822c\u5728\u6784\u9020\u51fd\u6570\u91cc\u6307\u5b9a\u987b\u8981\u6253\u5f00\u7684\u6587\u4ef6\u540d\uff1a<\/p>\n

\n

QFile(const QString & name)\nQFile(QObject * parent)\nQFile(const QString & name, QObject * parent)<\/code><\/pre>\n<\/p>\n

\u53c2\u6570name\u5c31\u662f\u987b\u8981\u6253\u5f00\u7684\u6587\u4ef6\u540d\uff0c\u6ce8\u610f\u5fc5\u987b\u662f\u5b9e\u9645\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u4e0d\u80fd\u662f\u53ea\u6709\u6587\u4ef6\u5939\u8def\u5f84\u3002parent\u662f\u7236\u5bf9\u8c61\u8868\u9488\u3002\u5bf9\u4e8e\u7b2c\u4e8c\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u6ca1\u6709\u6307\u5b9a\u6587\u4ef6\u540d\uff0c\u5fc5\u987b\u5728\u4ee5\u540e\u7684\u4ee3\u7801\u91cc\u7528\u5982\u4e0b\u51fd\u6570\u8bbe\u7f6e\u6587\u4ef6\u540d\uff1a<\/p>\n

\n

void QFile::setFileName(const QString & name)<\/code><\/pre>\n<\/p>\n

\u8bbe\u7f6e\u4e86\u6587\u4ef6\u540d\u4ee5\u540e\u624d\u4f1a\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u3002\u83b7\u53d6\u6587\u4ef6\u540d\u5c31\u7528fileName()\u51fd\u6570\uff0c\u4e0d\u5355\u72ec\u5217\u4e86\u3002<\/p>\n

\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u4f8b\u5982\u53ea\u8bfb\u6a21\u5f0f\u3001\u53ea\u5199\u6a21\u5f0f\u3001\u8bfb\u5199\u6a21\u5f0f\u7b49\uff0c\u6700\u5e38\u7528\u7684\u6253\u5f00\u51fd\u6570\u4e3a\uff1a<\/p>\n

\n

bool QFile::\u200bopen(OpenMode mode)<\/code><\/pre>\n<\/p>\n

OpenMode\u679a\u4e3e\u7c7b\u578b\u662f\u5728\u6cdb\u578bQIODevice\u5b9a\u4e49\u7684\uff0c\u6709\u5982\u4e0b\u6253\u5f00\u6a21\u5f0f\uff1a<\/p>\n

OpenMode\u679a\u4e3e\u5e38\u91cf<\/p>\n

\u6570\u503c<\/p>\n

\u63cf\u8ff0<\/p>\n

QIODevice::NotOpen<\/p>\n

0x0000<\/p>\n

\u7528\u4e8e\u8868\u793a\u8bbe\u5907\u6216\u6587\u4ef6\u4ecd\u672a\u6253\u5f00\u3002<\/p>\n

QIODevice::ReadOnly<\/p>\n

0x0001<\/p>\n

\u6309\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\u3002<\/p>\n

QIODevice::WriteOnly<\/p>\n

0x0002<\/p>\n

\u6309\u5199\u5165\u6a21\u5f0f\u6253\u5f00\uff0c\u6ce8\u610f\u5355\u72ec\u7528\u8fd9\u4e2a\u6a21\u5f0f\u4f1a\u53c2\u6742Truncate\u3002<\/p>\n

QIODevice::ReadWrite<\/p>\n

ReadOnly|WriteOnly<\/p>\n

\u6309\u8bfb\u53d6\u548c\u5199\u5165\u6a21\u5f0f\u6253\u5f00\uff0c\u65e2\u80fd\u8bfb\u4e5f\u80fd\u5199\u3002<\/p>\n

QIODevice::Append<\/p>\n

0x0004<\/p>\n

\u6309\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\uff0c\u6587\u4ef6\u5148\u524d\u5b58\u5728\u7684\u5185\u5bb9\u4e0d\u4f1a\u88ab\u8986\u76d6\uff0c\u65b0\u6570\u636e\u4ece\u6587\u4ef6\u672b\u5c3e\u5f00\u59cb\u5199\u5165\u3002<\/p>\n

QIODevice::Truncate<\/p>\n

0x0008<\/p>\n

\u5f3a\u5236\u6e05\u7a7a\u6587\u4ef6\u91cc\u66fe\u7ecf\u5b58\u7684\u65e7\u6570\u636e\uff0c\u65b0\u6570\u636e\u4ece\u96f6\u5f00\u59cb\u5199\u5165\u3002<\/p>\n

QIODevice::Text<\/p>\n

0x0010<\/p>\n

\u5728\u8bfb\u53d6\u65f6\uff0c\u628a\u884c\u5c3e\u7ed3\u675f\u7b26\u66f4\u6539\u4e3a'n'\uff1b\u5728\u5199\u5165\u65f6\uff0c\u628a\u884c\u5c3e\u7ed3\u675f\u7b26\u66f4\u6539\u4e3a\u672c\u5730\u7cfb\u7edf\u6362\u884c\u98ce\u683c\uff0c\u4f8b\u5982Windows\u6587\u672c\u6362\u884c\u662f\"rn\"<\/p>\n

QIODevice::Unbuffered<\/p>\n

0x0020<\/p>\n

\u5ffd\u89c6\u7f13\u51b2\u533a\uff0c\u76f4\u63a5\u8bfb\u5199\u8bbe\u5907\u6216\u6587\u4ef6\u3002\u9664\u975e\u662f\u5b9e\u65f6\u6027\u5f88\u5f3a\u7684\u7a0b\u5e8f\uff0c\u5426\u5219\u7528\u4e0d\u5230\u3002<\/p>\n

\u6587\u4ef6\u8bfb\u53d6\u65f6\uff0c\u5e38\u89c1\u7ec4\u5408\u5982\u4e0b\u8fb9\u4e24\u53e5\uff1a<\/p>\n

\n

file.open(QIODevice::ReadOnly);    \/\/\u4ee5\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\nfile.open(QIODevice::ReadOnly | QIODevice::Text);    \/\/\u786e\u5b9a\u662f\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff0c\u5e76\u4e14\u81ea\u52a8\u628a\u6362\u884c\u7b26\u4fee\u6539\u4e3a 'n'<\/code><\/pre>\n<\/p>\n

\u6ce8\u610f\u4ee5QIODevice::Text\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u65f6\uff0c\u8bfb\u5199\u7684\u6570\u636e\u4e0d\u4e00\u5b9a\u662f\u539f\u59cb\u6570\u636e\uff0c\u7531\u4e8eQFile\u624b\u52a8\u628a\u6362\u884c\u7b26\u505a\u4e86\u8f6c\u6362\uff0c\u8bfb\u53d6\u5f97\u5230\u7684\u7f13\u51b2\u533a\u6570\u636e\u4e0e\u539f\u59cb\u6587\u4ef6\u662f\u53ef\u80fd\u4e0d\u4e00\u6837\u7684\uff0c\u4f8b\u5982Windows\u6587\u672c\uff0c\u6362\u884c\u7b26\u662f\"rn\"\u4e24\u4e2a\u5b57\u7b26\uff0c\u7528QIODevice::Text\u8bfb\u53d6\u65f6\u53ea\u4f1a\u542c\u5230'n'\u4e00\u4e2a\u5b57\u7b26\uff1b\u5199\u5165\u65f6\u5c31\u53cd\u8fc7\u6765\uff0c\u4ee3\u7801\u5199\u5165\u4e00\u4e2a'n'\uff0c\u5b9e\u9645\u6587\u4ef6\u5c31\u662f\u4e24\u4e2a\u8fde\u7eed\u5b57\u7b26\"rn\"\uff0c\u6240\u4ee5\u8981\u6ce8\u610fQIODevice::Text\u6a21\u5f0f\u8bfb\u5199\u7684\u4e0d\u4e00\u5b9a\u662f\u539f\u59cb\u6570\u636e\u3002<\/p>\n

\u5bf9\u4e8e\u6587\u4ef6\u5199\u5165\u65f6\uff0c\u5176\u5e38\u7528\u6253\u5f00\u6a21\u5f0f\u5982\u4e0b\uff1a<\/p>\n

\n

file.open(QIODevice::WriteOnly);    \/\/\u4ee5\u53ea\u5199\u6a21\u5f0f\u6253\u5f00\uff0c\u8fd9\u4e2a\u6a21\u5f0f\u6697\u542b Truncate\uff0c\u4f1a\u6e05\u7a7a\u65e7\u6570\u636e\nfile.open(QIODevice::WriteOnly | QIODevice::Truncate);    \/\/\u53ea\u5199\u6a21\u5f0f\uff0c\u6e05\u7a7a\u65e7\u6570\u636e\nfile.open(QIODevice::WriteOnly | QIODevice::Append);     \/\/\u53ea\u5199\u548c\u8ffd\u52a0\u6a21\u5f0f\uff0c\u4e0d\u4f1a\u6e05\u7a7a\u65e7\u6570\u636e<\/code><\/pre>\n<\/p>\n

\u5047\u5982\u6587\u4ef6\u6253\u5f00\u65f6\u65e2\u8981\u8bfb\uff0c\u53c8\u8981\u5199\uff0c\u8fd9\u4e48\u5efa\u8bae\u7528\u5982\u4e0b\u6a21\u5f0f\uff1a<\/p>\n

\n

file.open(QIODevice::ReadWrite);    \/\/\u8bfb\u5199\u6a21\u5f0f\uff0c\u65e7\u6570\u636e\u4e0d\u4f1a\u6e05\u7a7a\uff0c\u53ef\u4ee5\u8bfb\u51fa\u6765<\/code><\/pre>\n<\/p>\n

\u6587\u4ef6\u6253\u5f00\u4ee5\u540e\uff0c\u53ef\u4ee5\u7528\u4eceQIODevice\u627f\u7ee7\u6765\u7684\u8bfb\u5199\u51fd\u6570\u64cd\u4f5c\u6587\u4ef6\uff0c\u6216\u5219\u7528QFile\u5bf9\u8c61\u8868\u9488\u6784\u9020QTextStream\u6216QDataStream\u6765\u8bfb\u5199\u6587\u4ef6\u3002<\/p>\n

\u4e0d\u4ec5\u91cc\u9762\u6700\u5e38\u7528\u7684\u6253\u5f00\u51fd\u6570\uff0c\u53e6\u5916\u8fd8\u6709\u4e24\u4e2a\u4e0d\u592a\u5e38\u7528\u7684\u6253\u5f00\u51fd\u6570\uff1a<\/p>\n

\n

bool QFile::\u200bopen(FILE * fh, OpenMode mode, FileHandleFlags handleFlags = DontCloseHandle)\nbool QFile::\u200bopen(int fd, OpenMode mode, FileHandleFlags handleFlags = DontCloseHandle)<\/code><\/pre>\n<\/p>\n

\u91cc\u9762\u7b2c\u4e00\u4e2a\u4e0d\u5e38\u7528\u6253\u5f00\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u6807\u51c6\u8f93\u5165\u6d41stdin\u3001\u6807\u51c6\u8f93\u51fa\u6d41stdout\u3001\u6807\u51c6\u9519\u8bef\u6d41stderr\uff0c\u6216\u5219\u5176\u4ed6\u6587\u4ef6\u53e5\u67c4\uff08Windows\u7cfb\u7edf\u4e2d\u53c2\u6570\u91cc\u7684fh\u53e5\u67c4\u5fc5\u987b\u4ee5\u4e8c\u8865\u7801\u6a21\u5f0f\u6253\u5f00\uff0c\u6253\u5f00\u53e5\u67c4\u65f6\u8981\u5e26'b'\u6a21\u5f0f\uff09\u3002\u6700\u540e\u7684\u53c2\u6570handleFlags\u901a\u5e38\u5c31\u7528\u4e0d\u5173\u6389\u7684DontCloseHandle\u5c31\u53ef\u4ee5\u4e86\uff0c\u82e5\u679c\u5e0c\u671bQFile\u5bf9\u8c61\u6790\u6784\u65f6\u624b\u52a8\u5173\u6389\u6587\u4ef6\u6216\u6d41\uff0c\u8fd9\u4e48\u53ef\u4ee5\u7528QFileDevice::AutoCloseHandle\u3002<\/p>\n

\u5bf9\u4e8eWindows\u5e73\u53f0\uff0c\u5047\u5982\u5e0c\u671b\u5728\u56fe\u5f62\u754c\u9762\u7a0b\u5e8f\u4e0a\u9762\u7528\u6807\u51c6\u7684\u8f93\u5165\u8f93\u51fa\u548c\u9519\u8bef\u6d41\uff0c\u8fd9\u4e48\u5fc5\u987b\u5728\u9879\u76ee\u6587\u4ef6\u52a0\u4e00\u53e5\uff1a<\/p>\n

CONFIG+=console<\/p>\n

\u91cc\u9762\u7b2c\u4e8c\u4e2a\u4e0d\u5e38\u7528open()\u51fd\u6570\u662f\u4ee5fd\u6587\u4ef6\u63cf\u8ff0\u7b26\u53c2\u6570\uff0c\u4e0eC\u8bed\u8a00\u4e0a\u9762\u7684\u6587\u4ef6\u8bfb\u5199\u7528\u5230\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7c7b\u4f3c\uff0c\u5047\u5982fd\u4e3a0\u662f\u6807\u51c6\u8f93\u5165\u6d41\uff0c\u4e3a1\u662f\u6807\u51c6\u8f93\u51fa\u6d41\uff0c\u4e3a2\u662f\u6807\u51c6\u9519\u8bef\u6d41\u3002<\/p>\n

open()\u51fd\u6570\u6253\u5f00\u6b63\u786e\u5c31\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defasle\uff0c\u6ce8\u610f\u5224\u5b9a\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4e4b\u540e\u518d\u8fdb\u884c\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\uff01<\/p>\n

\uff082\uff09\u8bfb\u5199\u51fd\u6570<\/p>\n

\u672c\u5c0f\u8282\u4e3b\u8981\u4ecb\u7ecd\u4eceQIODevice\u627f\u7ee7\u800c\u81f3\u7684\u8bfb\u5199\u51fd\u6570\uff0c\u8fd9\u79cd\u51fd\u6570\u8981\u5728QIODevice\u7c7b\u5e2e\u52a9\u6587\u6863\u624d\u4f1a\u627e\u5230\u3002<\/p>\n

\u9996\u5148\u662f\u7b80\u5355\u7684\u5b57\u8282\u8bfb\u5199\u51fd\u6570\uff1a<\/p>\n

\n

bool QIODevice::\u200bgetChar(char * c)<\/code><\/pre>\n<\/p>\n

\u53c2\u6570\u8868\u9488c\u5c31\u662f\u8bfb\u53d6\u7684\u4e00\u4e2a\u5b57\u8282\u5373\u5c06\u5b58\u5230\u7684\u53d8\u91cf\u8868\u9488\uff0c\u7a0b\u5e8f\u5458\u987b\u8981\u81ea\u5df1\u5148\u5b9a\u4e49\u4e00\u4e2achar\u53d8\u91cf\uff0c\u628a\u8fd9\u4e2a\u53d8\u91cf\u5730\u5740\u4f20\u9012\u7ed9\u200bgetChar()\u51fd\u6570\uff0c\u5047\u82e5\u8bfb\u53d6\u4e00\u5b57\u8282\u6210\u529f\u5c31\u8fd4\u56detrue\uff1b\u5047\u5982\u4e4b\u524d\u65e9\u5df2\u5230\u4e86\u6587\u4ef6\u672b\u5c3e\uff0c\u6ca1\u6709\u5b57\u8282\u53ef\u4ee5\u8bfb\u4e86\uff0c\u5c31\u8fd4\u56defalse\u3002\u200bgetChar()\u51fd\u6570\u6709\u4e00\u4e2a\u9006\u64cd\u4f5c\u51fd\u6570\uff1a<\/p>\n

\n

void QIODevice::\u200bungetChar(char c)<\/code><\/pre>\n<\/p>\n

\u8fd9\u4e2a\u51fd\u6570\u5c31\u662f\u628a\u4e4b\u524d\u8bfb\u53d6\u7684\u5b57\u8282c\uff08\u6b64\u6b21\u662f\u53d8\u91cf\uff0c\u4e0d\u662f\u8868\u9488\uff09\u653e\u56de\u5bb6\uff0c\u4f46\u662f\u5f53\u524d\u8bfb\u53d6\u6e38\u6807\u51cf\u4e00\uff0c\u8fd8\u539f\u5230\u8bfb\u53d6\u4e4b\u524d\u72b6\u6001\u3002\u6ce8\u610f\uff0c\u5047\u82e5c\u5b57\u8282\u4e0d\u7b49\u4e8e\u4e4b\u524d\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u503c\uff0c\u8fd9\u4e48\u200bungetChar()\u51fd\u6570\u64cd\u4f5c\u7ed3\u679c\u672a\u80fd\u9884\u77e5\uff0c\u6240\u4ee5\u4e0d\u8981\u4f7f\u7528\u200bungetChar()\u51fd\u6570\u66f4\u6539\u6587\u4ef6\uff01<\/p>\n

\u5199\u5165\u4e00\u4e2a\u5b57\u8282\u5230\u6587\u4ef6\u4e2d\uff0c\u5e94\u5f53\u4f7f\u7528\u51fd\u6570\uff1a<\/p>\n

\n

bool QIODevice::\u200bputChar(char c)<\/code><\/pre>\n<\/p>\n

\u8fd9\u4e2a\u51fd\u6570\u4f1a\u628a\u5b57\u8282c\u5199\u5165\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u6e38\u6807\u52a0\u4e00\u3002<\/p>\n

\u8fd9\u513f\u6211\u4eec\u4e13\u95e8\u8bb2\u4e00\u4e0b\u6587\u4ef6\u6e38\u6807\uff0c\u6587\u4ef6\u5728\u8bfb\u5199\u65f6\uff0c\u5171\u540c\u4f7f\u7528\u4e00\u4e2a\u60df\u4e00\u7684\u6e38\u6807\uff08QFile\u5185\u90e8\u6709\uff09\uff0c\u6211\u4eec\u8fd9\u513f\u968f\u610f\u53d6\u4e2a\u540d\u5b50\u53ebpos\uff08Position\uff09\uff0c\u8fd9\u4e2apos\u5728\u6587\u4ef6\u521a\u6253\u5f00\u65f6\u901a\u5e38\u5904\u4e8e\u6587\u4ef6\u5f00\u5934\u4f4d\u7f6e\uff1a<\/p>\n

linux \u65e5\u5fd7\u5207\u5272\u811a\u672c_linux \u6587\u4ef6\u5207\u5272_linux \u5207\u5272\u6587\u4ef6<\/p>\n

\u8bf4\u660e\u4e00\u4e0b\uff0c\u6587\u4ef6\u7684\u5927\u5c0f\u662f\u7528size()\u51fd\u6570\u83b7\u53d6\u7684\uff1a<\/p>\n

\n

qint64 QFile::\u200bsize() const<\/code><\/pre>\n<\/p>\n

\u6587\u4ef6\u5927\u5c0f\u4f7f\u7528qint64\u7c7b\u578b\u53d8\u91cf\u4fdd\u5b58\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4QFile\u6700\u5927\u652f\u63012^63-1==9,223,372,036,854,775,807\u5b57\u8282\u7684\u6587\u4ef6\uff0c\u6240\u4ee5\u4e0d\u7528\u64cd\u52b3QFile\u5bf9\u5927\u6587\u4ef6\u7684\u652f\u6301\u7279\u70b9\u3002<\/p>\n

QFile\u5f53\u524d\u6e38\u6807\u53ef\u4ee5\u7528\u51fd\u6570\u83b7\u53d6\uff1a<\/p>\n

\n

qint64 QFileDevice::\u200bpos() const<\/code><\/pre>\n<\/p>\n

\u5bf9\u4e8e\u6309\u5b57\u8282\u8bfb\u53d6\u7684\u51fd\u6570\u200bgetChar()\uff0c\u6bcf\u8c03\u7528\u4e00\u6b21\uff0c\u6587\u4ef6\u6e38\u6807\u5c31\u52a01\uff0c\u5047\u5982\u8fde\u7eed\u8c03\u7528\u4e86N\u6b21\uff0c\u6e38\u6807pos\u90fd\u4f1a\u8054\u901a\u5230\u53f3\u56fe\u6240\u793a\u4f4d\u7f6e\uff1a<\/p>\n

linux \u5207\u5272\u6587\u4ef6_linux \u6587\u4ef6\u5207\u5272_linux \u65e5\u5fd7\u5207\u5272\u811a\u672c<\/p>\n

\u4e4b\u524d\u4ecb\u7ecd\u7684\u200bgetChar()\u3001\u200bputChar()\uff0c\u5047\u5982\u8bfb\u5199\u6b63\u786e\u5c31\u4f1a\u4f7fpos\u624b\u52a8\u52a01\uff0cungetChar()\u51fd\u6570\u5047\u5982\u64cd\u4f5c\u6b63\u786e\uff0c\u8fd9\u4e48\u4f1a\u4f7fpos\u624b\u52a8\u51cf\u4e00\uff0c\u8fd9\u4e2a\u6e38\u6807\u90fd\u662fQFile\u624b\u52a8\u63a7\u5236\uff0c\u901a\u5e38\u4e0d\u987b\u8981\u81ea\u52a8\u8054\u901a\u6e38\u6807\u3002<\/p>\n

\u3010\u9886\u66f4\u591aQT\u5b66\u4e60\u8d44\u6599\uff0c\u70b9\u51fb\u4e0b\u65b9\u94fe\u63a5\u514d\u8d39\u53d1\u653e\u2193\u2193\uff0c\u5148\u7801\u4f4f\u4e0d\u8d70\u5931~\u3011<\/p>\n

\u70b9\u51fb\u2192\u9886\u53d6\u300c\u94fe\u63a5\u300d<\/p>\n

\u6211\u4eec\u5bf9\u6587\u4ef6\u8bfb\u53d6\u5230\u4e00\u5b9a\u7a0b\u5ea6\uff0c\u90fd\u4f1a\u8bfb\u5230\u6587\u4ef6\u672b\u5c3e\u4f4d\u7f6e\uff0c\u62b5\u8fbe\u672b\u5c3e\u4ee5\u540e\u5c31\u96be\u4ee5\u518d\u8bfb\u6570\u636e\u4e86\uff0c\u7531\u4e8e\u6e38\u6807\u65e9\u5df2\u8d85\u51fa\u8303\u56f4\uff1a<\/p>\n

linux \u65e5\u5fd7\u5207\u5272\u811a\u672c_linux \u6587\u4ef6\u5207\u5272_linux \u5207\u5272\u6587\u4ef6<\/p>\n

QFile\u5b50\u7c7b\u6709\u5feb\u6377\u51fd\u6570\u5224\u5b9a\u6587\u4ef6\u6e38\u6807\u662f\u5426\u5df2\u62b5\u8fbe\u6587\u4ef6\u672b\u5c3e\uff1a<\/p>\n

\n

bool QFileDevice::\u200batEnd() const<\/code><\/pre>\n<\/p>\n

\u5bf9\u4e8e\u5b57\u8282\u8bfb\u53d6\u51fd\u6570\uff0c\u6587\u4ef6\u6e38\u6807\u662f\u6309\u4e00\u5b57\u8282\u8054\u901a\u7684\uff0c\u5047\u5982\u8981\u8bfb\u53d6\u5927\u6bb5\u6570\u636e\u5757\uff0c\u8fd9\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e0b\u8fb9\u7684\u51fd\u6570\uff1a<\/p>\n

\n

qint64 QIODevice::\u200bread(char * data, qint64 maxSize)<\/code><\/pre>\n<\/p>\n

data\u4e00\u822c\u662f\u7a0b\u5e8f\u5458\u81ea\u52a8\u5206\u914d\u7684\u7f13\u51b2\u533alinux \u6587\u4ef6\u5207\u5272<\/strong>\uff0c\u4f8b\u5982char*buff=newchar[256];<\/p>\n

maxSize\u5c31\u662f\u6700\u591a\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u91cf\uff0c\u901a\u5e38\u662f\u81ea\u52a8\u5206\u914d\u7684\u7f13\u51b2\u533a\u5927\u5c0f\uff0c\u4f8b\u5982256\u3002<\/p>\n

\u8be5\u51fd\u6570\u8fd4\u56de\u503c\u901a\u5e38\u5c31\u662f\u6b63\u786e\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u91cf\uff0c\u7531\u4e8e\u5047\u5982\u6587\u4ef6\u524d\u9762\u82e5\u679c\u6ca1\u6709256\u5b57\u8282\uff0c\u8fd9\u4e48\u6709\u51e0\u4e2a\u5b57\u8282\u8bfb\u51e0\u4e2a\u5b57\u8282\u3002<\/p>\n

\u5047\u5982read()\u51fd\u6570\u5728\u8bfb\u53d6\u4e4b\u524d\u5c31\u5230\u4e86\u6587\u4ef6\u672b\u5c3e\u6216\u5219\u8bfb\u53d6\u9519\u8bef\uff0c\u8fd9\u4e48\u8fd4\u56de\u503c\u662f-1\u3002\u5bf9\u4e8e\u4f7f\u7528QIODevice::WriteOnly\u53ea\u5199\u6a21\u5f0f\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u4e00\u822c\u6587\u4ef6\u6e38\u6807\u603b\u662f\u6307\u5411\u6587\u4ef6\u672b\u5c3e\uff0c\u8fd9\u65f6\u4faf\u8c03\u7528read()\u6ca1\u610f\u4e49\uff0c\u6240\u4ee5read()\u8fd4\u56de\u503c\u5c31\u662f-1\u3002<\/p>\n

\u81ea\u52a8\u5206\u914d\u7f13\u51b2\u533a\u867d\u7136\u662f\u6bd4\u8f83\u9ebb\u70e6\u7684\u4e8b\u60c5\uff0c\u6211\u4eecQt\u539f\u751f\u6001\u7684\u8bfb\u53d6\u51fd\u6570\u5e94\u5f53\u7528\u4e0b\u8fb9\u8fd9\u4e2a\uff1a<\/p>\n

\n

QByteArray QIODevice::\u200bread(qint64 maxSize)<\/code><\/pre>\n<\/p>\n

\u8fd9\u513f\u7684read()\u51fd\u6570\u4f1a\u628a\u8bfb\u53d6\u7684\u5b57\u8282\u5b57\u6bb5\u5b58\u5230QByteArray\u5bf9\u8c61\u5e76\u8fd4\u56de\uff0c\u53c2\u6570\u91cc\u7684maxSize\u5c31\u662f\u6700\u591a\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u91cf\u3002\u8fd4\u56de\u7684QByteArray\u5bf9\u8c61\u4e0a\u9762\uff0c\u53ef\u4ee5\u7528QByteArray\u81ea\u5df1\u7684QByteArray::\u200bsize()\u51fd\u6570\u5224\u5b9a\u8bfb\u4e86\u591a\u5c11\u5b57\u8282\uff0c\u5047\u5982\u6587\u4ef6\u524d\u9762\u6ca1\u5b57\u8282\u53ef\u8bfb\u6216\u8bfb\u53d6\u9519\u8bef\uff0c\u8fd9\u4e48QByteArray\u89c4\u683c\u5c31\u662f0\u3002<\/p>\n

\n

QByteArray QIODevice::\u200breadAll()<\/code><\/pre>\n<\/p>\n

readAll()\u51fd\u6570\u770b\u540d\u5b50\u5c31\u6653\u5f97\uff0c\u628a\u6587\u4ef6\u7684\u5168\u90e8\u5185\u5bb9\u76f4\u63a5\u8bfb\u53d6\u5230QByteArray\u5bf9\u8c61\u4e4b\u540e\u8fd4\u56de\u3002<\/p>\n

\u53e6\u5916\u8fd8\u6709\u4e24\u4e2a\u66f4\u5b9e\u7528\u7684\u8bfb\u53d6\u884c\u7684\u51fd\u6570\uff1a<\/p>\n

\n

qint64 QIODevice::\u200breadLine(char * data, qint64 maxSize)\nQByteArray QIODevice::\u200breadLine(qint64 maxSize = 0)<\/code><\/pre>\n<\/p>\n

\u7b2c\u4e00\u4e2a\u200breadLine()\u662f\u7a0b\u5e8f\u5458\u81ea\u52a8\u5206\u914d\u7f13\u51b2\u533a\uff0c\u7b2c\u4e8c\u4e2a\u4e0d\u987b\u8981\u81ea\u52a8\u5206\u914d\u7f13\u51b2\u533a\u3002<\/p>\n

readLine()\u51fd\u6570\u5de5\u4f5c\u7279\u70b9\u6bd4\u8f83\u7279\u6b8a\uff0c\u5b83\u662f\u4ece\u6587\u4ef6\u6216\u8bbe\u5907\u4e0a\u9762\u8bfb\u53d6\u4e00\u884cASCII\u5b57\u7b26\uff0c\u6700\u591a\u8bfb\u53d6maxSize-1\u5b57\u8282\uff0c\u7531\u4e8e\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u9884\u7559\u7ed9\u5b57\u7b26\u4e32\u7ed3\u5c3eNULL\u5b57\u7b26\u3002<\/p>\n

\u8be5\u51fd\u6570\u8fd4\u56de\u503c\u662f\u771f\u5b9e\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u91cf\uff0c\u5047\u5982\u8bfb\u53d6\u51fa\u9519\u6216\u65e0\u6570\u636e\u53ef\u8bfb\u5c31\u8fd4\u56de-1\u3002<\/p>\n

\u200breadLine()\u603b\u4f1a\u5728\u5b9e\u9645\u8bfb\u53d6\u7684\u5b57\u7b26\u4e32\u672b\u5c3e\u4f1a\u624b\u52a8\u6dfb\u52a0\u4e00\u4e2a\u5b57\u7b26\u4e32\u7ec8\u7ed3\u7b260\u3002<\/p>\n

\u200breadLine()\u4f1a\u4ecd\u7136\u8bfb\u53d6\u6570\u636e\u76f4\u81f3\u5982\u4e0b\u4e09\u4e2a\u6761\u4ef6\u4e4b\u4e00\u6ee1\u8db3\uff1a<\/p>\n

\u2460\u7b2c\u4e00\u4e2a'n'\u5b57\u7b26\u8bfb\u53d6\u5230\u7f13\u51b2\u533a\u3002<\/p>\n

\u2461maxSize-1\u5b57\u8282\u6570\u5df2\u8bfb\u53d6\uff0c\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u9884\u7559\u7ed90\u3002<\/p>\n

\u2462\u6587\u4ef6\u6216\u8bbe\u5907\u8bfb\u53d6\u65e9\u5df2\u5230\u672b\u5c3e\u3002<\/p>\n

\u5bf9\u4e8e\u7b2c\u4e00\u4e2a\u4e2d\u6b62\u6761\u4ef6\uff0c\u771f\u5b9e\u8bfb\u53d6\u5230\u4e86'n'\u5b57\u7b26\uff0c\u8fd9\u4e48\u8fd9\u4e2a\u6362\u884c\u5b57\u7b26\u4f1a\u586b\u5145\u5230\u7f13\u51b2\u533a\u4e0a\u9762\uff1b<\/p>\n

\u5bf9\u4e8e\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a\u4e2d\u6b62\u6761\u4ef6\uff0c\u662f\u6ca1\u6709\u8bfb\u5230'n'\u5b57\u7b26\uff0c\u8fd9\u4e48\u8be5\u51fd\u6570\u4e0d\u4f1a\u624b\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\u5230\u672b\u5c3e\u3002<\/p>\n

\u8fd8\u6709\u4e00\u4e2a\u7279\u6b8a\u60c5\u51b5\u8981\u6ce8\u610f\uff0creadLine()\u51fd\u6570\u4f1a\u628aWindows\u7684\u6587\u4ef6\u6362\u884c\u98ce\u683c\"rn\"\u624b\u52a8\u66ff\u6362\u6539\u4e3a'n'\u3002<\/p>\n

\u200bread()\u548creadAll()\u3001\u200breadLine()\u51fd\u6570\u5c31\u4f1a\u8054\u901a\u6587\u4ef6\u6e38\u6807\uff0c\u5177\u4f53\u662f\u770b\u771f\u5b9e\u8bfb\u4e86\u591a\u5c11\u5b57\u8282\u3002<\/p>\n

\u4ee5\u4e0a\u4e3b\u8981\u662f\u8bfb\u64cd\u4f5c\uff0c\u5199\u64cd\u4f5c\u4e0d\u4ec5putChar()\uff0c\u8fd8\u6709\u5982\u4e0b\u4e09\u4e2a\u5199\u51fd\u6570\uff1a<\/p>\n

\n

qint64 QIODevice::\u200bwrite(const char * data, qint64 maxSize)<\/code><\/pre>\n<\/p>\n

data\u5c31\u662f\u7f13\u51b2\u533a\u6570\u636e\u8868\u9488qq linux\uff0cmaxSize\u662f\u6700\u591a\u5199\u5165\u7684\u5b57\u8282\u6570\u3002\u8fd4\u56de\u503c\u662f\u771f\u5b9e\u5199\u5165\u7684\u5b57\u8282\u6570\uff0c\u7531\u4e8e\u53ef\u80fd\u51fa\u73b0c\u76d8\u4e0d\u591f\u7684\u60c5\u51b5\u3002\u5047\u5982\u8fd4\u56de\u503c\u662f-1\uff0c\u8fd9\u4e48\u53ef\u80fd\u662f\u5199\u5165\u51fa\u9519\u6216\u5219\u65e0\u5199\u5165\u6743\u9650\u3002\u8fd9\u4e2a\u5199\u51fd\u6570\u4e0d\u5206\u8fa8data\u7f13\u51b2\u533a\u4e0a\u9762\u7684''\u5b57\u7b26\u548c\u666e\u901a\u5b57\u7b26\u4e32\u5b57\u7b26\uff0c\u90fd\u4e00\u80a1\u8111\u5199\u8fdb\u53bb\u3002<\/p>\n

\n

qint64 QIODevice::\u200bwrite(const char * data)<\/code><\/pre>\n<\/p>\n

\u8fd9\u7b2c\u4e8c\u4e2a\u51fd\u6570\u53c2\u6570\u6ca1\u6307\u5b9a\u7f13\u51b2\u533a\u5927\u5c0f\uff0c\u4f1a\u5c06\u53c2\u6570\u91cc\u7684data\u5f53\u6210''\u7ed3\u5c3e\u7684\u666e\u901a\u5b57\u7b26\u4e32\uff0c\u5199\u5165\u8be5\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u51fd\u6570\u7b49\u4ef7\u4e8e\u4e0b\u8fb9\u8fd9\u53e5\u4ee3\u7801\uff1a<\/p>\n

\n

QIODevice::write(data, qstrlen(data));<\/code><\/pre>\n<\/p>\n

\u7b2c\u4e09\u4e2a\u5199\u51fd\u6570\u867d\u7136\u66f4\u5e38\u7528\uff1a<\/p>\n

\n

qint64 QIODevice::\u200bwrite(const QByteArray & byteArray)<\/code><\/pre>\n<\/p>\n

byteArray\u4e0a\u9762\u6709\u591a\u5c11\u5b57\u8282\u5c31\u5199\u5165\u591a\u5c11\uff0c\u8fd9\u4e2a\u4e5f\u662f\u4e0d\u5206\u8fa8''\u5b57\u7b26\u548c\u666e\u901a\u5b57\u7b26\u4e32\u5b57\u7b26\uff0c\u90fd\u4e00\u80a1\u8111\u5199\u8fdb\u53bb\u3002<\/p>\n

\u5199\u64cd\u4f5c\u51fd\u6570\u4e5f\u5c31\u4f1a\u8054\u901a\u6587\u4ef6\u6e38\u6807pos\uff0c\u5177\u4f53\u662f\u770b\u5b9e\u9645\u5199\u5165\u4e86\u591a\u5c11\u5b57\u8282\u3002<\/p>\n

linux \u65e5\u5fd7\u5207\u5272\u811a\u672c_linux \u6587\u4ef6\u5207\u5272_linux \u5207\u5272\u6587\u4ef6<\/p>\n

\u901a\u5e38\u6211\u4eec\u90fd\u4e0d\u987b\u8981\u81ea\u52a8\u63a7\u5236\u6587\u4ef6\u6e38\u6807pos\uff0c\u800c\u4e14\u5047\u5982\u6709\u7279\u6b8a\u60c5\u51b5\uff0c\u987b\u8981\u81ea\u52a8\u79fb\u6e38\u6807\uff0c\u8fd9\u4e48\u901a\u8fc7\u4e0b\u8fb9\u51fd\u6570\uff1a<\/p>\n

\n

bool QFileDevice::\u200bseek(qint64 pos)<\/code><\/pre>\n<\/p>\n

\u200bseek\u51fd\u6570\u82e5\u679c\u6210\u529f\u8054\u901a\u6e38\u6807\uff0c\u8fd9\u4e48\u4f1a\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002\u6700\u597d\u4e0d\u8981\u7528seek\u51fd\u6570\u8054\u901a\u6e38\u6807\u5230\u8d85\u51fa\u6587\u4ef6\u89c4\u683c\u7684\u4f4d\u7f6e\uff0c\u8fd9\u6837\u4f1a\u9020\u6210\u96be\u4ee5\u610f\u6599\u7684\u7ed3\u679c\u3002<\/p>\n

\u5047\u5982\u5e0c\u671b\u8bbe\u7f6e\u6587\u4ef6\u89c4\u683c\uff0c\u63d0\u65e9\u5728c\u76d8\u4e0a\u5206\u914d\u7a7a\u95f4\uff0c\u53ef\u4ee5\u7528\u5982\u4e0b\u51fd\u6570\uff1a<\/p>\n

\n

bool QFile::\u200bresize(qint64 sz)<\/code><\/pre>\n<\/p>\n

\u53c2\u6570sz\u5c31\u662f\u65b0\u7684\u6587\u4ef6\u5927\u5c0f\uff0c\u5047\u82e5\u65b0\u5927\u5c0f\u6bd4\u65e7\u7684\u5927\uff0c\u8fd9\u4e48\u65b0\u589e\u7a7a\u95f4\u5185\u5bb9\u662f\u968f\u673a\u7684\uff0c\u987b\u8981\u7a0b\u5e8f\u5458\u4e4b\u540e\u81ea\u52a8\u586b\u5145\u6570\u636e\u3002\u91cd\u7f6e\u5927\u5c0f\u6210\u529f\u5c31\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002<\/p>\n

\u6587\u4ef6\u6253\u5f00\u548c\u8bfb\u5199\u64cd\u4f5c\u7ed3\u675f\u4ee5\u540e\uff0c\u5c31\u53ef\u4ee5\u5173\u6389\u6587\u4ef6\uff1a<\/p>\n

\n

void QFileDevice::\u200bclose()<\/code><\/pre>\n<\/p>\n

\u5728\u5199\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u5047\u5982\u987b\u8981\u7acb\u523b\u628aQt\u5185\u90e8\u5199\u7f13\u51b2\u533a\u7684\u6570\u636e\u5199\u5165c\u76d8\uff0c\u53ef\u4ee5\u8c03\u7528\uff1a<\/p>\n

\n

bool QFileDevice::\u200bflush()    \/\/\u8fd9\u4e2a\u51fd\u6570\u5f88\u5c11\u7528\u5230\uff0c\u6587\u4ef6\u5173\u95ed\u65f6\u81ea\u52a8\u4f1a\u6267\u884c flush<\/code><\/pre>\n<\/p>\n

\uff083\uff09\u6587\u4ef6\u5c5e\u6027\u548c\u6743\u9650\u7b49\u51fd\u6570<\/p>\n

QFile\u6709\u90e8\u4efd\u51fd\u6570\u663e\u7136\u4e0eQFileInfo\u7c7b\u529f\u80fd\u5dee\u4e0d\u591a\uff0c\u8fd9\u513f\u5927\u81f4\u8bb2\u89e3\u4e00\u4e0b\uff0c\u5bf9\u4e8e\u8fd9\u90e8\u4efd\u64cd\u4f5c\uff0c\u867d\u7136\u66f4\u5efa\u8bae\u7528QFileInfo\u6216QDir\u7c7b\u6765\u5b9e\u73b0\u3002<\/p>\n

\n

bool QFile::\u200bcopy(const QString & newName)<\/code><\/pre>\n<\/p>\n

\u628a\u5f53\u524d\u6587\u4ef6\u590d\u5236\u5230\u65b0\u6587\u4ef6newName\uff0c\u590d\u5236\u6210\u529f\u5c31\u8fd4\u56detrue\uff0c\u5426\u548b\u8fd4\u56defalse\u3002<\/p>\n

\u6ce8\u610f\u5047\u5982newName\u65b0\u6587\u4ef6\u4e4b\u524d\u65e9\u5df2\u5b58\u5728\uff0c\u8fd9\u4e48copy()\u51fd\u6570\u8fd4\u56defalse\uff0c\u5b83\u4e0d\u4f1a\u8986\u76d6\u65e7\u6587\u4ef6\u3002<\/p>\n

\u5f53\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u6e90\u6587\u4ef6\u624b\u52a8\u4f1a\u88ab\u5173\u6389\uff0c\u4e4b\u540e\u4f7f\u7528\u6e90\u6587\u4ef6\u6700\u597d\u518d\u91cd\u65b0\u6253\u5f00\u3002<\/p>\n

\n

bool QFile::\u200bexists() const<\/code><\/pre>\n<\/p>\n

\u5224\u5b9a\u5f53\u524d\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n

\n

bool QFile::\u200blink(const QString & linkName)<\/code><\/pre>\n<\/p>\n

\u4e3a\u5f53\u524d\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5feb\u6377\u65b9\u6cd5linkName\uff0c\u521b\u5efa\u6210\u529f\u8fd4\u56detrue\uff0c\u521b\u5efa\u5931\u8d25\u8fd4\u56defalse\u3002\u200blink()\u51fd\u6570\u4e5f\u4e0d\u4f1a\u8986\u76d6\u4e4b\u524d\u5df2\u5b58\u5728\u7684\u5feb\u6377\u65b9\u6cd5\u3002\u5bf9\u4e8eWindows\u7cfb\u7edf\uff0c\u5feb\u6377\u65b9\u6cd5\u540d\u5fc5\u987b\u4ee5.lnk\u7ed3\u5c3e\uff0c\u5426\u5219\u4f1a\u51fa\u9519\u3002<\/p>\n

\n

bool QFile::\u200bremove()<\/code><\/pre>\n<\/p>\n

\u5220\u6389\u5f53\u524d\u6587\u4ef6\uff0c\u5220\u6389\u4e4b\u524d\u6587\u4ef6\u4f1a\u624b\u52a8\u88ab\u5173\u6389\uff0c\u4e4b\u540e\u5220\u6389\u3002<\/p>\n

\n

bool QFile::\u200brename(const QString & newName)<\/code><\/pre>\n<\/p>\n

\u628a\u5f53\u524d\u6587\u4ef6\u91cd\u547d\u540d\u4e3a\u65b0\u540d\u5b50newName\uff0c\u82e5\u679c\u6210\u529f\u8fd4\u56detrue\uff0c\u5931\u8d25\u8fd4\u56defalse\u3002\u5047\u5982newName\u6587\u4ef6\u4e4b\u524d\u5df2\u5b58\u5728\uff0c\u8fd9\u4e48\u91cd\u547d\u540d\u4f1a\u5931\u8d25\uff0c\u65e7\u6587\u4ef6\u4e0d\u4f1a\u88ab\u8986\u76d6\u3002\u6587\u4ef6\u91cd\u547d\u540d\u4e4b\u524d\uff0c\u8be5\u6587\u4ef6\u4e5f\u4f1a\u624b\u52a8\u5173\u6389\u3002<\/p>\n

\n

QString QFile::\u200bsymLinkTarget() const<\/code><\/pre>\n<\/p>\n

\u5047\u5982\u5f53\u524d\u6587\u4ef6\u662f\u5feb\u6377\u65b9\u6cd5\u6587\u4ef6\uff0c\u8fd9\u4e48\u200bsymLinkTarget()\u8fd4\u56de\u539f\u59cb\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u6587\u4ef6\u540dlinux \u6587\u4ef6\u5207\u5272<\/strong>\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n

\n

Permissions QFile::\u200bpermissions() const\nbool QFile::\u200bsetPermissions(Permissions permissions)<\/code><\/pre>\n<\/p>\n

\u83b7\u53d6\u548c\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u7684\u51fd\u6570\uff0cPermissions\u679a\u4e3e\u53d8\u91cf\u4e0e7.1.3QFileInfo\u7c7b\u7684\u6743\u9650\u679a\u4e3e\u662f\u4e00\u6837\u7684\u3002<\/p>\n

\uff084\uff09QFile\u7c7b\u9759\u6001\u51fd\u6570<\/p>\n

\u6709\u597d\u51e0\u4e2a\u9759\u6001\u51fd\u6570\u4e0e\u524d\u9762\uff083\uff09\u91cc\u7684\u51fd\u6570\u91cd\u540d\uff0c\u53ea\u662f\u53c2\u6570\u4e00\u822c\u6bd4\u524d\u9762\u540c\u540d\u51fd\u6570\u591a\u4e00\u4e2a\uff0c\u591a\u7684\u53c2\u6570\u662f\u6e90\u6587\u4ef6\u540d\uff0c\u8fd9\u513f\u5c31\u4e0d\u5217\u51fa\u4e86\u3002<\/p>\n

\u9759\u6001\u51fd\u6570\u4e0a\u9762\uff0c\u6709\u4e09\u4e2a\u4e0e\u524d\u9762\u5185\u5bb9\u4e0d\u91cd\u590d\u7684\uff1a<\/p>\n

\n

QString QFile::\u200bdecodeName(const QByteArray & localFileName)\nQString QFile::\u200bdecodeName(const char * localFileName)<\/code><\/pre>\n<\/p>\n

\u8fd9\u4e24\u4e2a\u6587\u4ef6\u540d\u89e3\u7801\u51fd\u6570\u628a\u64cd\u4f5c\u7cfb\u7edf\u672c\u5730\u5316\u7684\u8def\u5f84\u6587\u4ef6\u540d\u8f6c\u4e3aQt\u6807\u51c6\u7684QString\u8def\u5f84\u6587\u4ef6\u540d\uff08\u8def\u5f84\u5206\u9694\u7b26\u662f'\/'\uff09\u3002<\/p>\n

\u5176\u5b9e\u4e5f\u6709\u6587\u4ef6\u540d\u7f16\u7801\u51fd\u6570\uff1a<\/p>\n

\n

QByteArray QFile::\u200bencodeName(const QString & fileName)<\/code><\/pre>\n<\/p>\n

\u8fd9\u4e2a\u51fd\u6570\u628aQt\u6807\u51c6\u7684QString\u6587\u4ef6\u540d\u7f16\u7801\u79f0\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u672c\u5730\u5316\u7684\u8def\u5f84\u6587\u4ef6\u540d\u3002<\/p>\n

\u5173\u4e8eQFile\u7c7b\u7684\u529f\u80fd\u51fd\u6570\u4ecb\u7ecd\u5230\u8fd9\uff0c\u4e0b\u8fb9\u6765\u77a7\u77a7\u793a\u4f8b\u3002<\/p>\n

\u8bfb\u6587\u4ef6<\/p>\n

\n

QFile file(\".\/student.txt\");\nif (!file.open(QIODevice::ReadOnly | QIODevice::Text))\n{\n    qDebug() << "\u6587\u4ef6\u6253\u5f00\u5931\u8d25";\n    return;\n}\n\/\/file.seek(0);\/\/\u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u5f00\u5934\nwhile (!file.atEnd())\n{\n    qDebug() << QString(file.readLine());  \/\/\u8bfb\u53d6\u6587\u4ef6\u4e2d\u4e00\u884c\n    \/\/qDebug() << QString(file.readAll()); \/\/\u8bfb\u53d6\u6587\u4ef6\u4e2d\u6240\u6709\u5185\u5bb9\n}<\/code><\/pre>\n<\/p>\n

QIODevice::Text\u6807\u5fd7\u544a\u8bc9Qt\u5c06windows\u98ce\u683c\u7684\u884c\u4e2d\u6b62\u7b26(\"rn\")\u8f6c\u6362\u4e3ac++\u98ce\u683c\u7684\u4e2d\u6b62\u7b26(\"n\")\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cQFile\u5047\u5b9a\u4e3a\u4e8c\u8865\u7801\uff0c\u5373\u5b83\u4e0d\u4f1a\u5bf9\u50a8\u5b58\u5728\u6587\u4ef6\u4e2d\u7684\u5b57\u8282\u6267\u884c\u4efb\u4f55\u8f6c\u6362\u3002<\/p>\n

\u5199\u6587\u4ef6<\/p>\n

\n

QFile file(\".\/what.txt\");\nif(!file.open(QIODevice::WriteOnly | QIODevice::Text))\n{\n    qDebug() << "\u6587\u4ef6\u6253\u5f00\u5931\u8d25";\n    return;\n}\nfile.write("\u5927\u5bb6\u597d\uff0c\u6211\u662fxxx");<\/code><\/pre>\n<\/p>\n

\u66f4\u6539.txt\u6587\u4ef6\u4e2d\u5185\u5bb9(\u8bfb\u6587\u4ef6\u4e0e\u5199\u6587\u4ef6)<\/p>\n

\n

QString strAll;\n    QStringList strList;\n    QFile readFile(\"C:\/Users\/Administrator\/Desktop\/r.txt\");\/\/\u81ea\u5b9a\u4e49\u8bfb\u53d6\u6587\u4ef6\u7684\u8def\u5f84\n    if(readFile.open((QIODevice::ReadOnly|QIODevice::Text)))\n    {\n        QTextStream stream(&readFile);\n        strAll = stream.readAll();\/\/\u628a\u6587\u4ef6\u6240\u6709\u4fe1\u606f\u8bfb\u51fa\u6765\u4fdd\u5b58\u5230strAll\u4e2d\n    }\n    readFile.close();\n    QFile writeFile(\"C:\/Users\/Administrator\/Desktop\/w.txt\");\/\/\u662f\u81ea\u5b9a\u4e49\u5199\u6587\u4ef6\u7684\u8def\u5f84\n    if(writeFile.open(QIODevice::WriteOnly|QIODevice::Text))\n    {\n        QTextStream stream(&writeFile);\n        strList = strAll.split(\"n\");\/\/\u4ee5\u6362\u884c\u7b26\u4e3a\u57fa\u51c6\u5206\u5272\u6587\u672c,\u4fdd\u5b58\u5230strList\u4e2d\n        for(int i=0;i<strList.count();i++)\/\/\u904d\u5386\u6bcf\u4e00\u884c\n        {\n            if(strList.at(i).contains("flag"))\/\/flag\u662f\u8981\u4fee\u6539\u7684\u5185\u5bb9\n            {\n                QString tempStr=strList.at(i);\n                tempStr.replace(0,tempStr.length(),"Hello");\/\/\u628aflag\u66ff\u6362\u6210Hello\n                stream<<tempStr<<'n';\n            }\n            \/\/\u5982\u679c\u6ca1\u6709\u627e\u5230\u8981\u66ff\u6362\u7684\u5185\u5bb9\uff0c\u7167\u5e38\u5199\u5165\n            else\n            {\n                if(i == strList.count()-1)\/\/\u5982\u679c\u5230\u4e86\u6587\u4ef6\u6700\u540e\u4e00\u884c,\u4e0d\u7528\u52a0"n"\n                {\n                    stream<<strList.at(i);\n                }\n                else\/\/\u5426\u5219\u6bcf\u4e00\u884c\u90fd\u52a0\u4e00\u4e2a"n"\n                {\n                    stream<<strList.at(i)<<'n';\n                }\n            }\n        }\n    }\n    writeFile.close();<\/code><\/pre>\n<\/p>\n

\u4e34\u65f6\u6587\u4ef6<\/p>\n

QTemporaryFile\u7528\u4e8e\u5b89\u5168\u5730\u521b\u5efa\u60df\u4e00\u7684\u4e34\u65f6\u6587\u4ef6<\/p>\n

\n

void tempFile()\n{\n    qDebug()<<QDir::tempPath();\n    QTemporaryFile tempfile;\n    if (!tempfile.open())\n    {\n        qDebug() << "\u6253\u5f00\u5931\u8d25"<<tempfile.errorString();\n        return;\n    }\n    QTextStream stream(&tempfile);\n    stream << "hello";\n    qDebug() << tempfile.fileName();\n}<\/code><\/pre>\n<\/p>\n

3.\u6d41\u64cd\u4f5c<\/p>\n

\u5728\u524d\u9762\u7684\u8bfb\u5199\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53d1\u89c9\u64cd\u4f5c\u8fd8\u662f\u592a\u8fc7\u590d\u6742\uff0c\u4e3a\u4e86\u7b80\u5316\u6587\u672c\u6587\u4ef6\u548c\u6570\u636e\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\uff0cQT\u63d0\u4f9b\u4e86QTextStream\u548cQDataStream\u8f85\u52a9\u7c7b\u3002<\/p>\n

QTextStream:\u53ef\u5c06\u5199\u5165\u7684\u6570\u636e\u5168\u90e8\u8f6c\u6362\u4e3a\u53ef\u8bfb\u6587\u672c\uff0cQDataStream:\u53ef\u5c06\u5199\u5165\u7684\u6570\u636e\u6309\u7167\u7c7b\u578b\u8f6c\u6362\u4e3a\u4e8c\u8865\u7801\u6570\u636e\u3002<\/p>\n

3.1QTextStream(\u6587\u672c\u6d41)<\/p>\n

\u6807\u51c6C++\u6709\u4e09\u4e2a\u5e38\u89c1\u7684\u8f93\u5165\u8f93\u51fa\u6d41\uff1aiostream\u5904\u7406\u547d\u4ee4<\/a>\u884c\u8f93\u5165\u8f93\u51fa\u3001fstream\u5904\u7406\u6587\u4ef6\u8f93\u51fa\u8f93\u51fa\u3001sstream\u5904\u7406\u663e\u5b58\u5b57\u7b26\u4e32\u6d41\u7684\u8f93\u5165\u8f93\u51fa\u3002\u4e00\u822c\u5c06\u6587\u672c\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5404\u7c7bC++\u53d8\u91cf\u7684\u8fc7\u7a0b\u662f\u8f93\u5165\uff0c\u628a\u663e\u5b58\u4e2dC++\u53d8\u91cf\u8868\u793a\u6210\u6587\u672c\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u662f\u8f93\u51fa\u3002<\/p>\n

Qt\u63d0\u4f9b\u4e86\u5f3a\u608d\u7684\u6587\u672c\u6d41QTextStream\uff0c\u540c\u65f6\u5b9e\u73b0\u4e86\u4e09\u79cdC++\u8f93\u5165\u8f93\u51fa\u6d41\u7684\u529f\u80fd\uff0c\u800c\u4e14\u8fd8\u6709\u529f\u80fd\u63d0\u9ad8\uff0c\u652f\u6301\u5404\u7c7b\u6587\u672c\u5b57\u7b26\u7f16\u7801\u3002QTextStream\u901a\u5e38\u7528\u4e8e\u64cd\u4f5c\u5404\u7c7b\u7f16\u7801\u683c\u5f0f\u7684\u6587\u672c\u6587\u4ef6\uff08QFile\u5bf9\u8c61\uff09\u6216\u5b57\u7b26\u4e32\uff08QString\u3001QByteArray\uff09\uff0c\u4e5f\u53ef\u4ee5\u6253\u5f00stdin\u3001stdout\u548cstderr\u547d\u4ee4\u884c\u7684\u8f93\u5165\u8f93\u51fa\uff0c\u5e76\u624b\u52a8\u5904\u7406\u672c\u5730\u5316\u7f16\u7801\u548cUnicode\u7f16\u7801\u3002<\/p>\n

QTextStream\u53ef\u4ee5\u5728QIODevice,QByteArray\u6216QString\u4e0a\u64cd\u4f5c\u3002\u4f7f\u7528QTextStream\u7684\u6d41\u64cd\u4f5c\u7b26\uff0c\u53ef\u4ee5\u4fbf\u6377\u5730\u8bfb\u548c\u5199\u8bcd\u7ec4\uff0c\u884c\u548c\u6570\u5b57\u3002\u5bf9\u4e8e\u751f\u6210\u6587\u672c\uff0cQTextStream\u652f\u6301\u6570\u7ec4\u586b\u5145\u548c\u5bf9\u9f50\u7684\u4f4e\u683c\u9009\u9879\uff0c\u4ee5\u53ca\u6570\u5b57\u7684\u4f4e\u683c\u3002<\/p>\n

\u3010\u9886\u66f4\u591aQT\u5b66\u4e60\u8d44\u6599\uff0c\u70b9\u51fb\u4e0b\u65b9\u94fe\u63a5\u514d\u8d39\u53d1\u653e\u2193\u2193\uff0c\u5148\u7801\u4f4f\u4e0d\u8d70\u5931~\u3011<\/p>\n

\u70b9\u51fb\u2192\u9886\u53d6\u300c\u94fe\u63a5\u300d<\/p>\n

3.1.1QTextStream\u6784\u9020\u51fd\u6570\u548c\u666e\u7814\u8bfb\u5199\u51fd\u6570<\/p>\n

\u4e00\u822c\u60c5\u51b5\u4e0bQTextStream\u81ea\u5df1\u4e0d\u4f1a\u4e3b\u52a8\u53bb\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u6240\u4ee5\u5b83\u7684\u6784\u9020\u51fd\u6570\u4e0d\u63a5\u6536\u6587\u4ef6\u540d\uff0c\u800c\u662f\u63a5\u6536QFile\u5b50\u7c7bQIODevice\u7684\u8868\u9488\uff0c\u53ef\u4ee5\u7528QFile\u5bf9\u8c61\u7684\u8868\u9488\u6784\u9020QTextStream\uff0c\u4f46\u987b\u8981\u6ce8\u610f\u5728\u6784\u9020QTextStream\u4e4b\u524d\u8981\u8c03\u7528QFile\u5bf9\u8c61\u7684open()\u51fd\u6570\u6309\u987b\u8981\u7684\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u5e76\u68c0\u6d4b\u662f\u5426\u6b63\u786e\u6253\u5f00\u3002<\/p>\n

\u7528\u4e8e\u8bfb\u5199\u6587\u4ef6\u7684QTextStream\u6784\u9020\u51fd\u6570\u4e3a\uff1a<\/p>\n

\n

 QTextStream(QIODevice * device)<\/code><\/pre>\n<\/p>\n

\u8bbe\u5907\u8868\u9488device\u987b\u8981\u63d0\u65e9\u7528open()\u51fd\u6570\u6253\u5f00\u3002<\/p>\n

\u5bf9\u4e8e\u6587\u4ef6\u8bbe\u5907\uff0c\u8fd8\u6709\u4e2a\u4e0d\u5e38\u7528\u7684\u6784\u9020\u51fd\u6570\uff1a<\/p>\n

\n

 QTextStream(FILE * fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite)<\/code><\/pre>\n<\/p>\n

fileHandle\u662f\u6587\u4ef6\u53e5\u67c4\uff0c\u7531\u4e8eQt\u4e0a\u9762\u7684\u6587\u4ef6\u901a\u5e38\u90fd\u7528QFile\u5904\u7406\uff0c\u6240\u4ee5\u51e0\u4e4e\u4e0d\u4f1a\u7528\u8fd9\u4e2a\u7b2c\u4e8c\u4e2a\u51fd\u6570\u5904\u7406\u6587\u4ef6\u3002\u7b2c\u4e8c\u4e2a\u6784\u9020\u51fd\u6570\u7684\u7528\u9014\u4e3b\u8981\u662f\u548c\u547d\u4ee4\u884c\u7684stdin\u3001stdout\u3001stderr\u4e09\u4e2a\u6587\u4ef6\u53e5\u67c4\u534f\u4f5c\uff0c\u5904\u7406\u547d\u4ee4\u884c\u7684\u8f93\u5165\u8f93\u51fa\u3002<\/p>\n

\u4ee5\u4e0a\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u7b2c\u4e00\u4e2a\u7528\u4e8e\u6587\u4ef6\u8bfb\u5199\uff08\u7c7b\u4f3cfstream\uff09\uff0c\u7b2c\u4e8c\u4e2a\u901a\u5e38\u7528\u4e8e\u547d\u4ee4\u884c\u8f93\u5165\u8f93\u51fa\uff08\u7c7b\u4f3ciostream\uff09\uff0c\u800cQTextStream\u7b2c\u4e09\u4e2a\u7528\u9014\u5c31\u662f\u5bf9\u663e\u5b58\u5b57\u7b26\u4e32\u7684\u4f4e\u683c\u8f93\u5165\u548c\u8f93\u51fa\uff08\u7c7b\u4f3csstream\uff09\u3002\u663e\u5b58\u5b57\u7b26\u4e32\u8f93\u5165\u8f93\u51fa\uff0c\u4f7f\u7528\u7684\u6784\u9020\u51fd\u6570\u4e3a\uff1a<\/p>\n

\n

QTextStream(QString * string, QIODevice::OpenMode openMode = QIODevice::ReadWrite)\nQTextStream(QByteArray * array, QIODevice::OpenMode openMode = QIODevice::ReadWrite)\n

<\/p>\nQTextStream(const QByteArray & array, QIODevice::OpenMode openMode = QIODevice::ReadOnly)<\/code><\/pre>\n<\/p>\n

\u6ce8\u610f\u540e\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5047\u82e5\u4f20\u9012QByteArray\u5bf9\u8c61\u7ed9\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u4e48\u8fd9\u4e2aQByteArray\u5bf9\u8c61\u662f\u53ea\u8bfb\u7684\u3002<\/p>\n

\u5047\u5982\u4f20\u9012QByteArray\u8868\u9488\u7ed9\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u4e48\u9ed8\u8ba4\u662f\u53ef\u8bfb\u53ef\u5199\u7684\u3002<\/p>\n

\u4e0d\u4ec5\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u6307\u5b9a\u6587\u4ef6\u8bbe\u5907\u6216\u663e\u5b58\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u66f4\u6539\uff1a<\/p>\n

\n

void QTextStream::\u200bsetDevice(QIODevice * device)\nvoid QTextStream::\u200bsetString(QString * string, QIODevice::OpenMode openMode = QIODevice::ReadWrite)<\/code><\/pre>\n<\/p>\n

\u76ee\u524d\u6ca1\u6709\u91cd\u65b0\u8bbe\u7f6eQByteArray\u7684\u51fd\u6570\uff0c\u53ea\u6709\u66f4\u6539\u6587\u4ef6\u8bbe\u5907\u548cQString\u7684\u51fd\u6570\u3002<\/p>\n

\u6211\u4eec\u8fd9\u513f\u7ea6\u5b9a\u4e00\u4e0b\u6982\u5ff5\uff0c\u65e0\u8bbaQTextStream\u6784\u9020\u51fd\u6570\u91cc\u662f\u6587\u4ef6\u8bbe\u5907\u6216\u663e\u5b58\u5b57\u7b26\u4e32\uff0c\u672c\u8282\u524d\u9762\u90fd\u628aQTextStream\u5bf9\u8c61\u79f0\u4e3a\u6587\u672c\u6d41\u3002<\/p>\n

\u5047\u5982\u6211\u4eec\u987b\u8981\u4ece\u6587\u672c\u6d41\u63d0\u53d6\u5404\u7c7bC++\u57fa\u672c\u6570\u503c\u7c7b\u578b\uff0c\u5982short\u3001int\u3001long\u3001double\uff0c\u8fd9\u79cd\u8bfb\u64cd\u4f5c\u901a\u5e38\u75287.3.2\u8282\u7684\u64cd\u4f5c\u5b50\u548c\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\uff0c\u5199\u5165\u6587\u672c\u6d41\u65f6\u4e5f\u4e00\u6837\u3002\u6d41\u64cd\u4f5c\u5b50\u548c\u8fd0\u7b97\u7b26\u4e4b\u5916\u7684\u8bfb\u5199\u51fd\u6570\u5c31\u662f\u672c\u5c0f\u8282\u8bf4\u7684\u666e\u7814\u8bfb\u5199\u51fd\u6570\u3002<\/p>\n

\u8bfb\u53d6\u4e00\u6574\u884c\u6587\u672c\u7684\u51fd\u6570\uff1a<\/p>\n

\n

QString QTextStream::\u200breadLine(qint64 maxlen = 0)<\/code><\/pre>\n<\/p>\n

QTextStream::\u200breadLine()\u8bfb\u53d6\u4e00\u884c\u6587\u672c\u7684\u65f6\u4faf\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32\u672b\u5c3e\u5254\u9664\u4e86\u6362\u884c\u7b26\uff08\"n\"\u6216\"rn\"\uff09\uff0c\u800c\u4e0a\u4e00\u8282QIODevice\u548cQFile\u7684readLine()\u51fd\u6570\u8fd4\u56de\u5b57\u7b26\u4e32\u672b\u5c3e\u5e26\u6709\u4e00\u4e2a\"n\"\uff0c\u8981\u6ce8\u610f\u5206\u8fa8\u3002<\/p>\n

\u5047\u5982\u8981\u8bfb\u53d6\u6587\u672c\u6d41\u4e2d\u6240\u6709\u5185\u5bb9\uff0c\u53ef\u4ee5\u7528\u5feb\u6377\u51fd\u6570\uff1a<\/p>\n

\n

QString QTextStream::\u200breadAll()<\/code><\/pre>\n<\/p>\n

\u5047\u5982\u5e0c\u671b\u4ece\u6587\u672c\u6d41\u4e2d\u8bfb\u53d6\u6307\u5b9a\u5bbd\u5ea6\u7684\u6587\u672c\uff0c\u53ef\u4ee5\u7528\u51fd\u6570\uff1a<\/p>\n

\n

QString QTextStream::\u200bread(qint64 maxlen)<\/code><\/pre>\n<\/p>\n

\u53c2\u6570\u91cc\u662f\u6700\u591a\u8bfb\u53d6\u7684\u5b57\u7b26\u6570\u91cf\uff0c\u5047\u5982\u6587\u672c\u6d41\u91cc\u7684\u6570\u636e\u4e0d\u591f\uff0c\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u5c31\u662f\u5269\u4f59\u7684\u5b57\u7b26\u6570\u3002<\/p>\n

\u5728\u4f7f\u7528QFile\u5bf9\u8c61\u8868\u9488\u6784\u9020QTextStream\u7684\u65f6\u4faf\uff0c\u4e00\u65e6\u628aQFile\u5bf9\u8c61\u8868\u9488\u4ea4\u7ed9QTextStream\u4ee5\u540e\uff0c\u5c31\u4e0d\u8981\u518d\u8c03\u7528QFile\u5bf9\u8c61\u81ea\u5df1\u7684\u8bfb\u5199\u51fd\u6570\u4e86\uff0c\u7531\u4e8eQTextStream\u4f7f\u7528\u81ea\u5df1\u7f13\u5b58\u7684\u6587\u4ef6\u6e38\u6807\uff0c\u5e95\u5c42QFile\u5bf9\u8c61\u81ea\u5df1\u53c8\u8fdb\u884c\u8bfb\u5199\u4ee5\u540e\uff0c\u4e24\u8005\u7684\u6587\u4ef6\u6e38\u6807\u5f88\u53ef\u80fd\u4e0d\u4e00\u81f4\uff0c\u4f1a\u5bfc\u81f4\u8bfb\u5199\u7684\u6df7\u4e71\u3002\u65e2\u7136\u4f7f\u7528QTextStream\u8bfb\u5199\u6587\u4ef6\uff0c\u90a3\u5c31\u53ea\u7528QTextStream\u7684\u8bfb\u5199\u51fd\u6570\u6216\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26\u3002<\/p>\n

QTextStream\u7684\u6587\u4ef6\u6e38\u6807\uff08\u867d\u7136\u5e94\u5f53\u53eb\u6587\u672c\u6d41\u6e38\u6807\uff09\u53ef\u4ee5\u7528\u51fd\u6570\u83b7\u53d6\u3001\u79fb\u52a8\uff0c\u4ee5\u53ca\u5224\u5b9a\u662f\u5426\u62b5\u8fbe\u6587\u4ef6\u672b\u5c3e\uff1a<\/p>\n

\n

qint64 QTextStream::\u200bpos() const    \/\/\u83b7\u53d6\u6e38\u6807\u4f4d\u7f6e\nbool QTextStream::\u200bseek(qint64 pos) \/\/\u79fb\u52a8\u6e38\u6807\u5230 pos\nbool QTextStream::\u200batEnd() const    \/\/\u662f\u5426\u5230\u8fbe\u6587 \u672c\u6d41\u672b\u5c3e<\/code><\/pre>\n<\/p>\n

\u5bf9\u4e8e\u6587\u672c\u6d41\u7684\u5199\u5165\u64cd\u4f5c\uff0cQTextStream\u538b\u6839\u6ca1\u6709write()\u51fd\u6570\uff0c\u5199\u5165\u64cd\u4f5c\u5168\u90e8\u662f\u901a\u8fc7\u8f93\u51fa\u8fd0\u7b97\u7b26>\u8fd0\u7b97\u7b26\u8fdb\u884c\u8f93\u5165\uff0c\u501f\u52a9\u8fd0\u7b97\u7b26\u5c31\u4e0d\u4e00\u4e2a\u4e2a\u5217\u51fa\u4e86\uff0c\u8bb2\u4e00\u4e0b\u5173\u4e8e\u5b57\u7b26\u4e32\u8bfb\u53d6\u7684\u4e09\u4e2a\uff1a<\/p>\n

\n

QTextStream & QTextStream::\u200boperator>>(QString & str)\nQTextStream & QTextStream::\u200boperator>>(QByteArray & array)\nQTextStream & QTextStream::\u200boperator>>(char * c)<\/code><\/pre>\n<\/p>\n

QTextStream\u5bf9\u4e8e\u5b57\u7b26\u4e32\u7684\u8bfb\u53d6\uff0c\u867d\u7136\u53ea\u8bfb\u53d6\u4e00\u4e2a\u8bcd\u7ec4\uff0c\u800c\u4e0d\u662f\u4e00\u6574\u884c\u3002\u4f8b\u5982\u5047\u5982\u6587\u672c\u6d41\u539f\u59cb\u4e3a\"Helloworld,aha!\"\uff0c\u5047\u5982\u7528\u524d\u9762\u4e09\u4e2a\u51fd\u6570\u4ece\u5934\u8bfb\u53d6\uff0c\u8fd9\u4e48\u8bfb\u53d6\u7684\u90fd\u53ea\u662f\u7b2c\u4e00\u4e2a\u8bcd\u7ec4\"Hello\"\uff0c\u5728\u6e90\u5934\u7684\u6587\u4ef6\u6216\u663e\u5b58\u5b57\u7b26\u4e32\u4e2d\uff0c\u6587\u672c\u6d41\u4e2d\u95f4\u90fd\u6ca1\u6709\u5b57\u7b26\u4e32\u7ec8\u7ed3\u7b26''\uff0c\u6240\u4ee5QTextStream\u5728\u8f93\u5165\u65f6\u6839\u636e\u7a7a\u767d\u5b57\u7b26\u5207\u5272\u8bcd\u7ec4\uff0c\u6bcf\u4f4d\u8bcd\u7ec4\u7b97\u4f5c\u4e00\u4e2a\u5c0f\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u7a7a\u767d\u5b57\u7b26\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26\u7b49\u7b49\u3002<\/p>\n

\u53e6\u5916\u5f53\u6587\u672c\u6d41\u7528>>\u8bfb\u53d6\u6328\u4e2a\u5b57\u7b26\u5230QChar\u6216char\u53d8\u91cf\u4e0a\u9762\u65f6\uff0c\u53ef\u4ee5\u7528<\/p>\n

\n

void QTextStream::\u200bskipWhiteSpace()<\/code><\/pre>\n<\/p>\n

\u8df3\u8fc7\u7a7a\u767d\u5b57\u7b26\uff0c\u5018\u82e5\u4e0d\u8df3\u8fc7\u7a7a\u767d\u5b57\u7b26\uff0c\u8fd9\u4e48QChar\u548cchar\u4e5f\u4f1a\u63a5\u6536\u5230\u7a7a\u767d\u5b57\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26\u7b49\u7b49\u3002<\/p>\n

\u5bf9\u4e8e\u5b57\u7b26\u4e32\u7684\u8f93\u51fa\u5c31\u6ca1\u6709\u524d\u9762\u8fd9\u4e9b\u95ee\u9898\u4e86\uff0c\u7531\u4e8e\u8f93\u51fa\u65f6\u53ef\u4ee5\u660e\u6670\u6653\u5f97\u53c2\u6570\u91cc\u7684QString\u3001QByteArray\u3001char*\u662f\u4ee5''\u7ed3\u5c3e\u7684\uff0c\u5728''\u540e\u9762\u7684\u5b57\u7b26\u4e32\u5c31\u4f1a\u88ab\u5199\u5165\u5230\u8f93\u51fa\u6587\u672c\u6d41\u4e2d\u3002<\/p>\n

\u8f93\u51fa\u6587\u672c\u6d41\u987b\u8981\u6ce8\u610f\u7684\u662fQTextStream\u6709\u8f83\u5927\u7684\u8f93\u51fa\u7f13\u51b2\u533a\uff0c\u5047\u5982\u7528stdout\u6216stderr\u6784\u9020QTextStream\u5bf9\u8c61\u590d\u5370\u8f93\u51fa\uff0c\u8981\u5e38\u7528flush\u6216endl\u64cd\u4f5c\u5b50\u51fd\u6570\u6e05\u7a7a\u7f13\u5b58\uff0c\u53ca\u65f6\u663e\u793a\u8f93\u51fa\uff1a<\/p>\n

\n

tsOut<<flush;\ntsOut<<endl;<\/code><\/pre>\n<\/p>\n

flush\u548cendl\u7684\u533a\u522b\u662fendl\u4f1a\u6362\u884c\uff0cflush\u4e0d\u6362\u884c\u3002<\/p>\n

\u6d41\u7684\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26\u7528\u4e0a\u53bb\u6bd4\u8f83\u7b80\u5355\uff0c\u4e0b\u8fb9\u91cd\u70b9\u77a7\u77a7\u63a7\u5236\u683c\u5f0f\u7684\u64cd\u4f5c\u5b50\uff0c\u64cd\u4f5c\u5b50\u672c\u8d28\u90fd\u662f\u51fd\u6570\uff0c\u4f8b\u5982\u5e38\u7528\u7684\u6362\u884cendl\uff1a<\/p>\n

\n

QTextStream & endl(QTextStream & stream)<\/code><\/pre>\n<\/p>\n

\u6211\u4eec\u8fd9\u513f\u628aQTextStream\u7684\u64cd\u4f5c\u5b50\u5927\u81f4\u7f57\u5217\u5e76\u89e3\u91ca\u4e00\u4e0b\uff1a<\/p>\n

\u2460\u6570\u503c\u7684\u8865\u7801\u548c\u7b26\u53f7\u3001\u5c0f\u6570\u70b9<\/p>\n

\u64cd\u4f5c\u5b50<\/p>\n

\u63cf\u8ff0<\/p>\n

bin<\/p>\n

\u8bfb\u5199\u4e8c\u8865\u7801\u6574\u6570\uff0c\u7b49\u540c\u4e8esetIntegerBase(2).<\/p>\n

oct<\/p>\n

\u8bfb\u5199\u516b\u8865\u7801\u6574\u6570\uff0c\u7b49\u540c\u4e8esetIntegerBase(8).<\/p>\n

dec<\/p>\n

\u8bfb\u5199\u5341\u8865\u7801\u6574\u6570\uff0c\u7b49\u540c\u4e8esetIntegerBase(10).<\/p>\n

hex<\/p>\n

\u8bfb\u5199\u5341\u516d\u8865\u7801\u6574\u6570\uff0c\u7b49\u540c\u4e8esetIntegerBase(16).<\/p>\n

showbase<\/p>\n

\u8f93\u51fa\u65f6\u663e\u793a\u6570\u503c\u8865\u7801\u524d\u7f00\uff0c\u4f8b\u5982\"0x\"\u3001\"0\"\u3001\"0b\"\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()|ShowBase).<\/p>\n

forcesign<\/p>\n

\u5f3a\u5236\u663e\u793a\u6b63\u53f7\uff08\u8d1f\u6570\u548c0\u540e\u9762\uff09\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()|ForceSign).<\/p>\n

forcepoint<\/p>\n

\u5f3a\u5236\u663e\u793a\u6574\u6570\u672b\u5c3e\u7684\u5c0f\u6570\u70b9\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()|ForcePoint).<\/p>\n

noshowbase<\/p>\n

\u4e0d\u663e\u793a\u8865\u7801\u7684\u524d\u7f00\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()&~ShowBase).<\/p>\n

noforcesign<\/p>\n

\u4e0d\u5f3a\u5236\u663e\u793a\u6b63\u53f7\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()&~ForceSign).<\/p>\n

noforcepoint<\/p>\n

\u4e0d\u5f3a\u5236\u663e\u793a\u5c0f\u6570\u70b9\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()&~ForcePoint).<\/p>\n

uppercasebase<\/p>\n

\u6570\u503c\u8865\u7801\u7684\u524d\u7f00\u4e3a\u5c0f\u5199\uff0c\u5982\"0X\"\u3001\"0B\"\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()|UppercaseBase).<\/p>\n

uppercasedigits<\/p>\n

\u57fa\u6570\u5c0f\u4e8e10\u8865\u7801\u7684\u6570\u503c\u4e0a\u9762\u7684\u5b57\u6bcd\u5c0f\u5199\uff0c\u4f8b\u598216\u8865\u7801\u7684\"FE00\"\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()|UppercaseDigits).<\/p>\n

lowercasebase<\/p>\n

\u6570\u503c\u8865\u7801\u7684\u524d\u7f00\u5927\u5199\uff0c\u5982\"0x\"\u3001\"0b\"\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()&~UppercaseBase).<\/p>\n

lowercasedigits<\/p>\n

\u57fa\u6570\u5c0f\u4e8e10\u8865\u7801\u7684\u6570\u503c\u4e0a\u9762\u7684\u5b57\u6bcd\u5927\u5199\uff0c\u4f8b\u598216\u8865\u7801\u7684\"fe00\"\uff0c\u7b49\u540c\u4e8esetNumberFlags(numberFlags()&~UppercaseDigits).<\/p>\n

\u2461\u6d6e\u70b9\u6570\u683c\u5f0f<\/p>\n

\u64cd\u4f5c\u5b50<\/p>\n

\u63cf\u8ff0<\/p>\n

fixed<\/p>\n

\u4ee5\u5b9a\u70b9\u6570\u663e\u793a\u5b9e\u6570\uff08\u4f8b\u5982float\u3001double\u7c7b\u578b\uff09\uff0c\u4f8b\u59821024.1234\uff0c\u7b49\u540c\u4e8esetRealNumberNotation(FixedNotation).<\/p>\n

scientific<\/p>\n

\u4ee5\u79d1\u5b66\u8ba1\u6570\u6cd5\u663e\u793a\u5b9e\u6570\uff0c\u4f8b\u59821.0241234e3\uff0c\u7b49\u540c\u4e8esetRealNumberNotation(ScientificNotation).<\/p>\n

qSetRealNumberPrecision(intprecision)<\/p>\n

\u8fd9\u662f\u5168\u5c40\u64cd\u4f5c\u5b50\uff0c\u5e26\u4e00\u4e2a\u6574\u578b\u53c2\u6570\uff0c\u6307\u5b9a\u5b9e\u6570\u663e\u793a\u7684\u7cbe\u5ea6\u4f4d\u6570\uff0c\u7b49\u540c\u4e8eQTextStream::setRealNumberPrecision(precision).<\/p>\n

qSetRealNumberPrecision(int)\u4f1a\u5bf9\u540e\u7eed\u6240\u6709\u5b9e\u6570\u53d8\u91cf\u8f93\u51fa\u90fd\u751f\u6548\uff0c\u76f4\u81f3\u88ab\u6539\u53d8\u4e3a\u6b62\uff0c\u5b9e\u6570\u7cbe\u5ea6\u9ed8\u8ba4\u662f6\u4f4d\uff0c\u5047\u5982\u5e0c\u671b\u8fd8\u539f\u5c31\u8bbe\u7f6e\u7cbe\u5ea6\u4e3a6\u3002\u6ce8\u610f\u4f7f\u7528\u64cd\u4f5c\u5b50qSetRealNumberPrecision(int)\u65f6\u6700\u597d\u4e00\u5757\u660e\u6670\u6307\u5b9a\u4f7f\u7528fixed\u6216\u5219scientific\uff0c\u8fd9\u6837\u663e\u793a\u7ed3\u679c\u624d\u662f\u7b26\u5408\u9884\u671f\u7684\u5c0f\u6570\u4f4d\u7cbe\u5ea6\u3002<\/p>\n

\u2462\u57df\u5bbd\u548c\u5bf9\u9f50\u65b9\u6cd5\u3001\u586b\u5145\u5b57\u7b26<\/p>\n

\u64cd\u4f5c\u5b50<\/p>\n

\u63cf\u8ff0<\/p>\n

qSetFieldWidth(intwidth)<\/p>\n

\u5168\u5c40\u64cd\u4f5c\u5b50\uff0c\u5e26\u4e00\u4e2a\u6574\u578b\u53c2\u6570\uff0c\u8bbe\u5b9a\u53d8\u91cf\u8f93\u51fa\u540e\u7684\u663e\u793a\u957f\u5ea6\uff0c\u4e0d\u8db3\u7684\u7528\u586b\u5145\u5b57\u7b26\u8865\u8db3\uff08\u9ed8\u8ba4\u7528\u7a7a\u683c\u8865\uff09\uff0c\u7b49\u540c\u4e8eQTextStream::setFieldWidth(width).<\/p>\n

qSetPadChar(QCharch)<\/p>\n

\u5168\u5c40\u64cd\u4f5c\u5b50\uff0c\u5e26\u4e00\u4e2a\u5b57\u7b26\u53c2\u6570\uff0c\u5047\u5982\u8bbe\u7f6e\u7684\u57df\u5bbd\u6bd4\u53d8\u91cf\u663e\u793a\u987b\u8981\u7684\u957f\u5ea6\u66f4\u5bbd\uff0c\u5c31\u7528\u8fd9\u4e2a\u586b\u5145\u5b57\u7b26\u5f25\u8865\uff08\u9ed8\u8ba4\u662f\u7a7a\u683c''\uff09\uff0c\u7b49\u540c\u4e8eQTextStream::setPadChar(ch).<\/p>\n

left<\/p>\n

\u8bbe\u5b9a\u57df\u5bbd\u6bd4\u53d8\u91cf\u663e\u793a\u987b\u8981\u7684\u66f4\u5bbd\u65f6\uff0c\u53d8\u91cf\u663e\u793a\u65f6\u5de6\u5bf9\u9f50\uff0c\u53f3\u4fa7\u7a7a\u4f4d\u8865PadChar\uff0c\u7b49\u540c\u4e8esetFieldAlignment(AlignLeft).<\/p>\n

right<\/p>\n

\u8bbe\u5b9a\u57df\u5bbd\u6bd4\u53d8\u91cf\u663e\u793a\u987b\u8981\u7684\u66f4\u5bbd\u65f6\uff0c\u53d8\u91cf\u663e\u793a\u65f6\u53f3\u5bf9\u9f50\uff0c\u53f3\u4fa7\u7a7a\u4f4d\u8865PadChar\uff0c\u7b49\u540c\u4e8esetFieldAlignment(AlignRight).<\/p>\n

center<\/p>\n

\u8bbe\u5b9a\u57df\u5bbd\u6bd4\u53d8\u91cf\u663e\u793a\u987b\u8981\u7684\u66f4\u5bbd\u65f6\uff0c\u53d8\u91cf\u663e\u793a\u65f6\u5c45\u4e2d\u5bf9\u9f50\uff0c\u4e24\u4fa7\u7a7a\u4f4d\u8865PadChar\uff0c\u7b49\u540c\u4e8esetFieldAlignment(AlignCenter).<\/p>\n

\u6ce8\u610f\uff1aqSetFieldWidth(int)\u8bbe\u7f6e\u57df\u5bbd\u4ee5\u540e\uff0c\u5018\u82e5\u4e0d\u91cd\u65b0\u8bbe\u7f6e\uff0c\u5b83\u5c31\u4ecd\u7136\u751f\u6548\uff0c\u5bf9\u540e\u7eed\u6240\u6709\u53d8\u91cf\u8f93\u51fa\u90fd\u597d\u4f7f\uff0c\u5305\u62ecendl\u5c31\u4f1a\u8865\u8db3\u53d8\u4e3a\u6307\u5b9a\u5bbd\u5ea6\u3002\u5047\u5982\u8981\u8fd8\u539f\u6210\u4e0d\u8bbe\u7f6e\u57df\u5bbd\u7684\u72b6\u6001\uff0c\u5c31\u8c03\u7528\u64cd\u4f5c\u5b50qSetFieldWidth(0)\u3002<\/p>\n

qSetPadChar(QChar)\u8bbe\u7f6e\u586b\u5145\u5b57\u7b26\u4ee5\u540e\u4e5f\u662f\u4ecd\u7136\u6301\u7eed\u751f\u6548\uff0c\u76f4\u81f3\u88ab\u91cd\u65b0\u8bbe\u7f6e\uff0c\u5047\u5982\u8fd8\u539f\u6210\u7a7a\u683c\uff0c\u5c31\u8c03\u7528\u64cd\u4f5c\u5b50qSetPadChar(QChar(0x20))\u3002<\/p>\n

\u3010\u9886\u66f4\u591aQT\u5b66\u4e60\u8d44\u6599\uff0c\u70b9\u51fb\u4e0b\u65b9\u94fe\u63a5\u514d\u8d39\u53d1\u653e\u2193\u2193\uff0c\u5148\u7801\u4f4f\u4e0d\u8d70\u5931~\u3011<\/p>\n

\u70b9\u51fb\u2192\u9886\u53d6\u300c\u94fe\u63a5\u300d<\/p>\n

\u2463\u5176\u4ed6\u64cd\u4f5c\u5b50<\/p>\n

\u64cd\u4f5c\u5b50<\/p>\n

\u63cf\u8ff0<\/p>\n

endl<\/p>\n

\u6dfb\u52a0\u6362\u884c\u7b26\u5e76\u6e05\u7a7a\u8f93\u51fa\u7f13\u5b58\uff0c\u628a\u7f13\u51b2\u6570\u636e\u90fd\u8bb2\u5230\u8f93\u51fa\u6587\u672c\u6d41\uff0c\u7b49\u540c\u4e8estream\u8fd0\u7b97\u7b26\u91cd\u8f7d\u51fd\u6570\uff0cQDataStream\u63d0\u4f9b\u4e86\u53e6\u5916\u51e0\u4e2a\u8bfb\u5199\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bfb\u5199\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u5757\uff0c\u9996\u5148\u662f\u4e00\u5bf9writeBytes()\u548creadBytes()\u51fd\u6570\uff1a<\/p>\n

\n

QDataStream & QDataStream::\u200bwriteBytes(const char * s, uint len) \/\/\u53d8\u91cf len \u4e5f\u4f1a\u5199\u5165\u6570\u636e\u6d41\nQDataStream & QDataStream::\u200breadBytes(char *& s, uint & l)      \/\/l\u6570\u503c\u662f\u4ece\u6570\u636e\u6d41\u91cc\u8bfb\u51fa\u6765\u7684\uff0c\u5c31\u662f\u4e0a\u9762\u7684 len<\/code><\/pre>\n<\/p>\n

\u5bf9\u4e8e\u5b57\u8282\u5199\u5165\u51fd\u6570\uff0c\u53c2\u6570\u91cc\u7684s\u662f\u8981\u8f93\u51fa\u7684\u5b57\u8282\u7f13\u51b2\u533a\uff0clen\u662f\u5199\u5165\u6570\u636e\u7684\u5bbd\u5ea6\uff0c\u8fd9\u513fs\u4e0a\u9762\u9488\u5bf9\u7eaf\u5b57\u8282\u6570\u636e\uff0c\u4e0d\u7ba1\u4e0a\u9762\u6709\u6ca1\u6709''\uff0c\u90fd\u5199\u5165\u6570\u636e\u6d41\u3002<\/p>\n

\u5bf9\u4e8e\u5b57\u8282\u8bfb\u53d6\u51fd\u6570\uff0cs\u662f\u8868\u9488\u53d8\u91cf\u7684\u5f15\u7528\uff0cl\u662f\u8bfb\u53d6\u7684\u5b57\u8282\u6570\uff0cs\u8868\u9488\u4e0d\u987b\u8981\u7a0b\u5e8f\u5458\u5206\u914d\u7a7a\u95f4\uff0c\u7531\u200breadBytes()\u51fd\u6570\u81ea\u5df1new[]\u4e00\u6bb5\u7f13\u51b2\u533a\uff0c\u4e4b\u540e\u628a\u7f13\u51b2\u533a\u8868\u9488\u5f62\u53c2\u7ed9s\uff1b\u53c2\u6570l\u662f\u51fd\u6570\u7684\u8fd4\u56de\u53d8\u91cf\uff0c\u662f\u771f\u5b9e\u8bfb\u53d6\u5230\u7684\u5b57\u8282\u6570\u3002\u51fd\u6570\u8fd4\u56de\u4ee5\u540es\u8868\u9488\u53d8\u91cf\u6307\u5411\u7684\u7f13\u51b2\u533a\u9700\u7a0b\u5e8f\u5458\u81ea\u52a8delete[]\u3002<\/p>\n

\u200bwriteBytes()\u51fd\u6570\u5728\u505a\u4e32\u884c\u5316\u65f6\uff0c\u4f1a\u5148\u5199quint32\u7c7b\u578b\u7684\u6570\u636e\u5bbd\u5ea6\uff0c\u4e4b\u540e\u5199\u5165\u771f\u5b9e\u7684\u7f13\u51b2\u533a\u6570\u636e\u5230\u6570\u636e\u6d41\u3002\u200breadBytes()\u4e5f\u662f\u5148\u8bfb\u53d6\u5b57\u8282\u94fe\u8868\u5bbd\u5ea6\uff0c\u8be5\u51fd\u6570\u81ea\u5df1new[]\u4e00\u7247\u7a7a\u95f4\uff0c\u628a\u524d\u9762\u7684\u771f\u5b9e\u6570\u636e\u8bfb\u5230\u7f13\u51b2\u533a\u3002<\/p>\n

writeBytes()\u548c\u200breadBytes()\u51fd\u6570\u4e0e\u5b57\u7b26\u4e32\u8bfb\u5199\u8fd0\u7b97\u7b26\u91cd\u8f7d\u51fd\u6570\u6709\u7c7b\u4f3c\u7684\u5730\u65b9\uff1a<\/p>\n

\n

QDataStream & QDataStream::\u200boperator<>(char *& s)<\/code><\/pre>\n<\/p>\n

\u5728\u505a\u4e32\u884c\u5316\u65f6\uff0c\u90fd\u662f\u5148\u5199\u4e00\u4e2aquint32\u7c7b\u578b\u7684\u5bbd\u5ea6\u5230\u6570\u636e\u6d41\uff0c\u4e4b\u540e\u5199\u771f\u5b9e\u6570\u636e\uff0c\u8bfb\u7684\u65f6\u4faf\u5c31\u662f\u5148\u8bfb\u5bbd\u5ea6\uff0c\u4e4b\u540e\u6309\u7167\u5bbd\u5ea6new[]\u7f13\u51b2\u533a\uff0c\u628a\u6570\u636e\u8bfb\u5230\u7f13\u51b2\u533a\u3002\u8fd9\u4e24\u5bf9\u51fd\u6570\u7684\u533a\u522b\u5c31\u662fwriteBytes()\u548c\u200breadBytes()\u9488\u5bf9\u5b57\u8282\u94fe\u8868\uff0c\u4e0d\u7ba1\u662f\u4e0d\u662f''\uff0c\u90fd\u5f53\u6210\u666e\u901a\u5b57\u8282\u8bfb\u5199\uff1b>\u8bfb\u5199\u5b57\u7b26\u4e32\u65f6\u9047\u89c1''\u5c31\u622a\u81f3\u4e86\uff0c\u4f46\u662f''\u4e0d\u4f1a\u5199\u5165\u5230\u6570\u636e\u6d41\u3002<\/p>\n

QDataStream\u8fd8\u63d0\u4f9b\u4e86\u66f4\u88f8\u7684\u8bfb\u5199\u51fd\u6570\uff0c\u4e0b\u8fb9\u8fd9\u5bf9\u8bfb\u5199\u51fd\u6570\u662f\u4e0d\u628a\u5b57\u8282\u94fe\u8868\u5bbd\u5ea6\u53d8\u91cf\u5199\u5165\u5230\u6570\u636e\u6d41\u7684\uff0c\u4ec5\u4ec5\u5199\u5165\u539f\u59cb\u7684\u7f13\u51b2\u533a\u6570\u636e\uff1a<\/p>\n

\n

int QDataStream::\u200bwriteRawData(const char * s, int len)\nint QDataStream::\u200breadRawData(char * s, int len)<\/code><\/pre>\n<\/p>\n

\u8fd9\u5bf9\u51fd\u6570\u4e0e\u4e4b\u524d\u4e00\u5bf9writeBytes()\u548c\u200breadBytes()\u51fd\u6570\u533a\u522b\u6709\u4e24\u70b9\uff1a\u7b2c\u4e00\uff0c\u4e0d\u5199\u6570\u636e\u5bbd\u5ea6\u53d8\u91cf\u5230\u6570\u636e\u6d41\uff0c\u53ea\u8bfb\u5199\u6700\u539f\u59cb\u7684\u6570\u636e\u5757\uff1b\u7b2c\u4e8c\uff0c\u200breadRawData()\u81ea\u5df1\u4e0d\u4f1a\u5206\u914d\u7f13\u51b2\u533a\uff0c\u5fc5\u987b\u7531\u7a0b\u5e8f\u5458\u63d0\u65e9\u5206\u914d\u7f13\u51b2\u533a\u7ed9s\uff0c\u7136\u540e\u4f20\u9012\u7ed9readRawData()\u51fd\u6570\u3002<\/p>\n

QDataStream\u4e0eQTextStream\u4e5f\u6709\u7c7b\u4f3c\u7684\u5730\u65b9\uff0c\u5c31\u662f\u6d41\u7684\u72b6\u6001\uff0c\u5047\u5982\u6570\u636e\u6d41\u7684\u8f93\u5165\u6b21\u5e8f\u4e0e\u8f93\u51fa\u6b21\u5e8f\u4e0d\u5339\u914d\uff0c\u8fd9\u4e48\u4f1a\u51fa\u73b0\u72b6\u6001\u9519\u8bef\uff0c\u83b7\u53d6\u6d41\u7684\u72b6\u6001\u51fd\u6570\u4e3a\uff1a<\/p>\n

\n

Status QDataStream::\u200bstatus() const<\/code><\/pre>\n<\/p>\n

\u72b6\u6001\u679a\u4e3e\u7c7b\u578b\u4e5f\u6709\u56db\u4e2a\uff1a<\/p>\n

Status\u679a\u4e3e\u5e38\u91cf<\/p>\n

\u6570\u503c<\/p>\n

\u63cf\u8ff0<\/p>\n

QDataStream::Ok<\/p>\n

0<\/p>\n

\u6b63\u5e38\u64cd\u4f5c\u72b6\u6001\uff0c\u6ca1\u51fa\u9519\u3002<\/p>\n

QDataStream::ReadPastEnd<\/p>\n

1<\/p>\n

\u5e95\u5c42\u8bbe\u5907\u5df2\u62b5\u8fbe\u672b\u5c3e\uff0c\u4f8b\u5982\u6587\u4ef6\u672b\u5c3e\uff0c\u65e0\u6570\u636e\u53ef\u8bfb\u4e86\u3002<\/p>\n

QDataStream::ReadCorruptData<\/p>\n

2<\/p>\n

\u8bfb\u5165\u4e86\u8150\u5316\u6570\u636e\uff0c\u5178\u578b\u7684\u5c31\u662f\u8f93\u5165\u6d41\u8bfb\u53d6\u6b21\u5e8f\u4e0e\u8f93\u51fa\u6d41\u6b21\u5e8f\u4e0d\u4e00\u6837\u3002<\/p>\n

QDataStream::WriteFailed<\/p>\n

3<\/p>\n

\u672a\u80fd\u5411\u5e95\u5c42\u8bbe\u5907\u5199\u5165\u6570\u636e\uff0c\u4f8b\u5982\u6587\u4ef6\u662f\u53ea\u8bfb\u7684\u3002<\/p>\n

\u5728QDataStream\u6570\u636e\u6d41\u51fa\u73b0\u9519\u8bef\u72b6\u6001\u4ee5\u540e\uff0c\u53ef\u4ee5\u91cd\u65b0\u8bbe\u7f6e\u6d41\u7684\u72b6\u6001\uff1a<\/p>\n

\n

void QDataStream::\u200bsetStatus(Status status) \/\/\u8bbe\u7f6e\u6d41\u4e3a\u53c2\u6570\u91cc\u6267\u884c\u7684\u72b6\u6001\nvoid QDataStream::\u200bresetStatus()         \/\/\u91cd\u7f6e\u4e3a\u539f\u59cb\u7684 Ok \u72b6\u6001<\/code><\/pre>\n<\/p>\n

linux \u6587\u4ef6\u5207\u5272_linux \u65e5\u5fd7\u5207\u5272\u811a\u672c_linux \u5207\u5272\u6587\u4ef6<\/p>\n

QDataStream\u6d41\u7684\u6392\u9664\u5904\u7406\u5c31\u6bd4\u8f83\u9ebb\u70e6\u4e86\uff0c\u5c3d\u7ba1\u53ef\u4ee5\u8df3\u8fc7\u4e00\u5b9a\u7684\u88f8\u5b57\u8282\u6570\uff1a<\/p>\n

\n

int QDataStream::\u200bskipRawData(int len)<\/code><\/pre>\n<\/p>\n

\u8fd9\u4e2a\u51fd\u6570\u8df3\u8fc7\u6700\u539f\u59cb\u7684\u88f8\u6570\u636elen\u5bbd\u5ea6\u7684\u5b57\u8282\uff0c\u8fd4\u56de\u503c\u662f\u771f\u5b9e\u8df3\u8fc7\u7684\u5b57\u8282\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u4e0e\u200breadRawData()\u914d\u5408\u4f7f\u7528\uff0c\u5e76\u4e14\u5728\u5904\u7406\u6d41\u9519\u8bef\u65f6\u672a\u5fc5\u6709\u7528\uff0c\u7531\u4e8e\u8df3\u8fc7\u6307\u5b9a\u7684\u5b57\u8282\u6570\u91cf\u7136\u540e\uff0c\u6d41\u7684\u6570\u636e\u5230\u54ea\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\u6216\u5bf9\u8c61\u4e86\uff0c\u65e0\u6cd5\u786e\u5b9a\u3002QDataStream\u8bfb\u53d6\u9519\u8bef\u65f6\uff0c\u6700\u597d\u7684\u505a\u6cd5\u662f\u76f4\u63a5\u63d0\u793a\u51fa\u9519\uff0c\u524d\u9762\u7684\u4e0d\u8bfb\u53d6\uff0c\u7531\u4e8e\u4e32\u884c\u5316\u6570\u636e\u6d41\u9ed8\u8ba4\u662f\u4e0d\u80fd\u968f\u673a\u5316\u8bfb\u5199\u7684\u3002<\/p>\n

QDataStream\u662f\u4f7f\u7528Qt\u81ea\u5df1\u72ec\u6709\u7684\u4e32\u884c\u5316\u6570\u636e\u6253\u5305\u5f62\u5f0f\uff0c\u5b83\u6ca1\u6709\u4efb\u4f55\u7684\u683c\u5f0f\u64cd\u4f5c\u5b50\uff0c\u7531\u4e8e\u8f93\u5165\u8f93\u51fa\u683c\u5f0f\u5168\u662f\u5916\u7f6e\u7684\uff0c\u4f46QDataStream\u6709\u81ea\u5df1\u517c\u5bb9\u7684Qt\u7248\u672c\u53f7\u3002\u968f\u7740Qt\u7248\u672c\u7684\u66f4\u65b0\uff0cQDataStream\u6253\u5305\u6570\u636e\u7684\u65b9\u6cd5\u4e5f\u4f1a\u66f4\u65b0\uff0c\u56e0\u800cQDataStream\u6709\u4e00\u5927\u5806\u517c\u5bb9\u7684\u7248\u672c\u53f7\uff0c\u4ece\u6700\u65e9\u7684Qt_1_0\u5230\u6700\u65b0\u7684Qt_5_4\uff0c\u7248\u672c\u53f7\u679a\u4e3e\u5e38\u91cf\u7684\u547d\u540d\u89c4\u5219\u5c31\u662fQt_\u5927\u7248\u672c\u53f7_\u5c0f\u7248\u672c\u53f7\u3002<\/p>\n

\u5047\u5982\u8d1f\u8d23\u8f93\u51fa\u7684\u7a0b\u5e8f\u4e0e\u8d1f\u8d23\u8f93\u5165\u7684\u7a0b\u5e8f\uff0c\u4f7f\u7528\u7684Qt\u7248\u672c\u662f\u4e00\u81f4\u7684\uff0c\u5c31\u4e0d\u987b\u8981\u8bbe\u7f6e\u7248\u672c\u53f7\u3002\u5e76\u4e14\u5047\u5982\u8f93\u51fa\u548c\u8f93\u5165\u7a0b\u5e8f\u7684Qt\u7248\u672c\u4e0d\u4e00\u81f4\uff0c\u4f8b\u5982\u8f93\u51fa\u7a0b\u5e8f\u4f7f\u7528\u7684\u662fQt4.8.*\uff0c\u8f93\u5165\u7a0b\u5e8f\u4f7f\u7528\u7684\u662fQt5.4.*\uff0c\u8fd9\u4e48\u5c31\u5e94\u5f53\u5411\u65e7\u7248\u672c\u517c\u5bb9\uff0c\u5728\u505a\u8bfb\u53d6\u53d8\u91cf\u6216\u5bf9\u8c61\u4e4b\u524d\uff0c\u8bbe\u7f6e\u7248\u672c\u53f7\uff1a<\/p>\n

\n

void QDataStream::\u200bsetVersion(int v)<\/code><\/pre>\n<\/p>\n

\u8981\u517c\u5bb9Qt4.8.*\uff0c\u8fd9\u4e48\u5c31\u628a\u53c2\u6570\u8bbe\u4e3aQDataStream::Qt_4_8\u3002<\/p>\n

\u968f\u7740Qt\u7248\u672c\u53f7\u7684\u66f4\u65b0\uff0cC++\u57fa\u672c\u7c7b\u578b\u7684\u53d8\u91cf\u8bfb\u5199\u89c4\u5219\u662f\u56fa\u5b9a\u7684\uff0c\u4f8b\u5982qint32\u5c31\u662f\u8bfb\u51994\u4e2a\u5b57\u8282\uff0c\u6240\u4ee5QDataStream\u7248\u672c\u53f7\u5bf9C++\u57fa\u672c\u7c7b\u578b\u53d8\u91cf\u8bfb\u5199\u867d\u7136\u6ca1\u5f71\u54cd\u3002QDataStream\u7248\u672c\u53f7\u5f71\u54cd\u7684\u662fQt\u81ea\u5df1\u7c7b\u5bf9\u8c61\u7684\u8bfb\u5199\u89c4\u5219\uff0c\u4f8b\u5982\u4e4b\u540e\u5047\u5982\u65b0\u589e\u4e86\u4e00\u4e2a\u7c7bQXXXXData\uff0c\u8fd9\u4e48\u53ea\u6709\u65b0\u7248\u672c\u7684Qt\u4e2dQDataStream\u80fd\u591f\u8bfb\u5199\u8fd9\u4e2a\u65b0\u7c7bQXXXXData\u5bf9\u8c61\u3002<\/p>\n

\u793a\u4f8b:<\/p>\n

\n

QByteArray data;\nQDataStream stream(&data,QIODevice::ReadWrite);\nstream << "image" << 1024 << "ABC";\n\/\/"[x00x00x00x06imagex00]   [x00x00x04x00]  [x00x00x00x04" "ABCx00"] 22\nqDebug() << data << data.size();<\/code><\/pre>\n<\/p>\n

\u4f7f\u7528QDataStream\u4e32\u884c\u5316\u6570\u636e\uff0c\u5047\u5982\u6570\u636e\u662f\u5b57\u7b26\u4e32\u5219\u4f1a\u5728\u4e0a\u9762\u75284\u4e2a\u5b57\u8282\u8868\u660e\u5b57\u7b26\u4e32\u5bbd\u5ea6\uff0c\u5018\u82e5\u662f\u6574\u6570\u5219\u76f4\u63a5\u50a8\u5b58\u3002<\/p>\n

4.QFileInfo4.1\u7b80\u4ecb<\/p>\n

QFileInfo\u7c7b\u63d0\u4f9b\u4e0e\u7cfb\u7edf\u65e0\u5173\u7684\u6587\u4ef6\u4fe1\u606f\uff0cQFileInfo\u63d0\u4f9b\u4e86\u5173\u4e8e\u6587\u4ef6\u7684\u540d\u79f0\u548c\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u4f4d\u7f6e(\u8def\u5f84)\u3001\u5b83\u7684\u8bbf\u95ee\u6743\u9650\u4ee5\u53ca\u5b83\u662f\u76ee\u5f55\u8fd8\u662f\u7b26\u53f7\u94fe\u63a5\u7b49\u4fe1\u606f\u3002\u6587\u4ef6\u7684\u5927\u5c0f\u548c\u6700\u540e\u66f4\u6539\/\u8bfb\u53d6\u65f6\u95f4\u4e5f\u53ef\u7528\u3002<\/p>\n

4.2\u5e38\u7528\u6280\u5de7<\/p>\n

\n

bool isDir() const               \/\/.\u5982\u679c\u8be5\u5bf9\u8c61\u6307\u5411\u76ee\u5f55\u6216\u6307\u5411\u76ee\u5f55\u7684\u7b26\u53f7\u94fe\u63a5\uff0c\u5219\u8fd4\u56detrue;\u5426\u5219\u8fd4\u56defalse\u3002\nbool isFile() const              \/\/.\u5982\u679c\u8be5\u5bf9\u8c61\u6307\u5411\u6587\u4ef6\u6216\u6307\u5411\u6587\u4ef6\u7684\u7b26\u53f7\u94fe\u63a5\uff0c\u5219\u8fd4\u56detrue\u3002\u5982\u679c\u5bf9\u8c61\u6307\u5411\u7684\u4e0d\u662f\u6587\u4ef6\uff0c\u6bd4\u5982\u76ee\u5f55\uff0c\u5219\u8fd4\u56defalse\u3002\nbool isHidden() const            \/\/.\u5982\u679c\u8fd9\u662f\u4e00\u4e2a\u201c\u9690\u85cf\u201d\u6587\u4ef6\uff0c\u5219\u8fd4\u56detrue;\u5426\u5219\u8fd4\u56defalse\u3002\nbool isSymLink() const           \/\/.\u5982\u679c\u8be5\u5bf9\u8c61\u6307\u5411\u7b26\u53f7\u94fe\u63a5\uff0c\u5219\u8fd4\u56detrue;\u5426\u5219\u8fd4\u56defalse\u3002\nQString symLinkTarget() const    \/\/.\u8fd4\u56de\u7b26\u53f7\u94fe\u63a5\u6307\u5411\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u5982\u679c\u5bf9\u8c61\u4e0d\u662f\u7b26\u53f7\u94fe\u63a5\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u3002\nQString suffix() const           \/\/.\u8fd4\u56de\u6587\u4ef6\u7684\u540e\u7f00(\u6269\u5c55\u540d)\u3002\nQString completeSuffix() const   \/\/.\u8fd4\u56de\u6587\u4ef6\u7684\u5b8c\u6574\u540e\u7f00(\u6269\u5c55\u540d)\u3002 \nQString fileName() const         \/\/.\u8fd4\u56de\u6587\u4ef6\u7684\u540d\u79f0(\u4e0d\u5305\u62ec\u8def\u5f84)\u3002\nQString baseName() const         \/\/.\u8fd4\u56de\u6587\u4ef6\u7684\u57fa\u672c\u540d\u79f0\uff0c\u4e0d\u5305\u542b\u8def\u5f84\u3002\nQString filePath() const         \/\/.\u8fd4\u56de\u6587\u4ef6\u540d\uff0c\u5305\u62ec\u8def\u5f84(\u53ef\u4ee5\u662f\u7edd\u5bf9\u7684\u6216\u76f8\u5bf9\u7684)\u3002\nQString absoluteFilePath() const \/\/.\u8fd4\u56de\u5305\u542b\u6587\u4ef6\u540d\u7684\u7edd\u5bf9\u8def\u5f84\u3002\nQString path() const             \/\/.\u8fd4\u56de\u6587\u4ef6\u7684\u8def\u5f84\u3002\u8fd9\u4e0d\u5305\u62ec\u6587\u4ef6\u540d\u3002\nqint64 size() const              \/\/.\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u8fd4\u56de\u6587\u4ef6\u5927\u5c0f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u65e0\u6cd5\u83b7\u53d6\uff0c\u5219\u8fd4\u56de0\u3002\nQDateTime birthTime() const      \/\/.\u8fd4\u56de\u6587\u4ef6\u521b\u5efa\/\u751f\u6210\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\nQDateTime lastModified() const   \/\/.\u8fd4\u56de\u6587\u4ef6\u6700\u540e\u4fee\u6539\u7684\u65e5\u671f\u548c\u672c\u5730\u65f6\u95f4\u3002<\/code><\/pre>\n<\/p>\n

\u793a\u4f8b:<\/p>\n

\n

QFileInfo info(file);\nQFileInfo info(\"..\/QFile-test\/what.txt\");\nqDebug() << info.size();         \/\/\u6587\u4ef6\u5927\u5c0f\nqDebug() << info.absoluteFilePath(); \/\/\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84(\u5305\u62ec\u6587\u4ef6\u540d)\nqDebug() << info.absolutePath();     \/\/\u7edd\u5bf9\u8def\u5f84(\u4e0d\u5305\u62ec\u6587\u4ef6\u540d)\nqDebug() << info.absoluteDir();      \/\/\u7edd\u5bf9\u8def\u5f84 QDir\nqDebug() << info.path();             \/\/\u6587\u4ef6\u8def\u5f84\nqDebug() << info.filePath();         \/\/\u8fd4\u56de\u6587\u4ef6\u540d\uff0c\u5305\u62ec\u8def\u5f84(\u53ef\u4ee5\u662f\u7edd\u5bf9\u8def\u5f84\u4e5f\u53ef\u4ee5\u662f\u76f8\u5bf9\u8def\u5f84)\u3002  \nif(info.is)\nif (info.isFile()) \/\/\u5982\u679c\u662f\u6587\u4ef6\n{\n    qDebug() << info.fileName(); \/\/\u5e26\u540e\u7f00\u7684\u6587\u4ef6\u540d\n    qDebug() << info.baseName(); \/\/\u4e0d\u5e26\u540e\u7f00\u7684\u6587\u4ef6\u540d\n    qDebug() << info.suffix();   \/\/\u83b7\u53d6\u6587\u4ef6\u540e\u7f00\n}<\/code><\/pre>\n<\/p>\n

5.QDir5.1\u7b80\u4ecb<\/p>\n

QDir\u7c7b\u63d0\u4f9b\u5bf9\u76ee\u5f55\u7ed3\u6784\u53ca\u5176\u5185\u5bb9\u7684\u8bbf\u95ee\u3002<\/p>\n

QDir\u7528\u4e8e\u64cd\u4f5c\u8def\u5f84\u540d\u3001\u8bbf\u95ee\u6709\u5173\u8def\u5f84\u548c\u6587\u4ef6\u7684\u4fe1\u606f\u4ee5\u53ca\u64cd\u4f5c\u5e95\u5c42\u6587\u4ef6\u7cfb\u7edf\u3002\u5b83\u4e5f\u53ef\u4ee5\u62ff\u6765\u8bbf\u95eeQt\u7684\u8d44\u6e90\u7cfb\u7edf\u3002<\/p>\n

Qt\u4f7f\u7528\u201c\/\u201d\u4f5c\u4e3a\u901a\u7528\u76ee\u5f55\u5206\u9694\u7b26\uff0c\u5982\u540c\u5728url\u4e2d\u4f7f\u7528\u201c\/\u201d\u4f5c\u4e3a\u8def\u5f84\u5206\u9694\u7b26\u4e00\u6837\u3002\u5018\u82e5\u60a8\u603b\u662f\u4f7f\u7528\u201c\/\u201d\u4f5c\u4e3a\u76ee\u5f55\u5206\u9694\u7b26\uff0cQt\u5c06\u8f6c\u6362\u60a8\u7684\u8def\u5f84\u4ee5\u7b26\u5408\u5e95\u5c42\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n

QDir\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u6307\u5411\u6587\u4ef6\u3002\u7edd\u5bf9\u8def\u5f84\u4ee5\u76ee\u5f55\u5206\u9694\u7b26\u5f00\u59cb(\u5728Windows\u4e0b\uff0c\u53ef\u4ee5\u5728\u4e0a\u9762\u52a0\u4e0a\u9a71\u52a8\u5668\u89c4\u8303)\u3002\u76f8\u5bf9\u6587\u4ef6\u540d\u4ee5\u76ee\u5f55\u540d\u6216\u6587\u4ef6\u540d\u5f00\u5934\uff0c\u5e76\u6307\u5b9a\u76f8\u5bf9\u4e8e\u5f53\u524d\u76ee\u5f55\u7684\u8def\u5f84\u3002<\/p>\n

5.2QDir\u529f\u80fd<\/p>\n

(1)\u3001\u76ee\u5f55\u5206\u9694\u7b26\u7edf\u4e00\u4f7f\u7528\u2019\/\u2019<\/p>\n

(2)\u3001\u80fd\u591f\u5bf9\u76ee\u5f55\u8fdb\u884c\u4efb\u610f\u64cd\u4f5c\uff08\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\uff09<\/p>\n

\n

bool mkdir(const QString &dirName) const\nbool mkpath(const QString &dirPath) const\n        \nbool rmdir(const QString &dirName) const    \/\/\u5220\u9664\u5b50\u76ee\u5f55(\u5fc5\u987b\u4e3a\u7a7a\u624d\u80fd\u5220\u9664)\nbool rmpath(const QString &dirPath) const   \/\/\u5220\u9664\u8def\u5f84(\u5fc5\u987b\u4e3a\u7a7a\u624d\u80fd\u5220\u9664)\nbool remove(const QString &fileName)        \/\/\u5220\u9664\u6307\u5b9a\u6587\u4ef6\nbool removeRecursively()                    \/\/\u5220\u9664\u76ee\u5f55\uff0c\u5305\u62ec\u5b83\u7684\u6240\u6709\u5185\u5bb9 \nbool rename(const QString &oldName, const QString &newName) \/\/\u91cd\u547d\u540d<\/code><\/pre>\n<\/p>\n

(3)\u3001\u80fd\u591f\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6761\u76ee\uff08\u6587\u4ef6\u548c\u6587\u4ef6\u5939\uff09<\/p>\n

\n

QFileInfoList entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const\nQFileInfoList entryInfoList(QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const\nQStringList entryList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const\nQStringList entryList(QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const<\/code><\/pre>\n<\/p>\n

(4)\u3001\u83b7\u53d6\u5e38\u7528\u8def\u5f84<\/p>\n

\n

\/\/\u8fd4\u56de\u5e94\u7528\u7a0b\u5e8f\u5f53\u524d\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\nQDir current()\nQString currentPath()\n\/\/\u8fd4\u56de\u7528\u6237\u4e3b\u76ee\u5f55  C:\/Users\/Maye\nQDir home()\nQString homePath()\n \n\/\/\u8fd4\u56de\u7cfb\u7edf\u7684\u4e34\u65f6\u76ee\u5f55\u3002   \nQDir temp()\nQString tempPath()\n    \n\/\/\u8fd4\u56de\u6839\u76ee\u5f55\u5217\u8868 C:\/  D:\/ ...\nQFileInfoList drives()<\/code><\/pre>\n<\/p>\n

(5)\u3001\u8ba1\u7b97\u76ee\u5f55\u7684\u5927\u5c0f<\/p>\n

\n

quint32 dirSize(const QString& dirName)\n{\n    QDir dir(dirName);\n    if (!dir.exists())\n        return ~0;\n    quint32 size = 0;\n    for (QFileInfo& info : dir.entryInfoList(QDir::Filter::NoDotAndDotDot | QDir::Files | QDir::Dirs))\n    {\n        qDebug() << info.fileName();\n        if (info.isFile())\n        {\n            size += info.size();\n        }\n        else\n        {\n            size += dirSize(info.filePath());\n        }\n    }\n    return size;\n}<\/code><\/pre>\n<\/p>\n

\u3010\u9886\u66f4\u591aQT\u5b66\u4e60\u8d44\u6599\uff0c\u70b9\u51fb\u4e0b\u65b9\u94fe\u63a5\u514d\u8d39\u53d1\u653e\u2193\u2193\uff0c\u5148\u7801\u4f4f\u4e0d\u8d70\u5931~\u3011<\/p>\n

\u70b9\u51fb\u2192\u9886\u53d6\u300c\u94fe\u63a5\u300d<\/p>\n

6.QFileSystemWatcher6.1\u7b80\u4ecb<\/p>\n

QFileSystemWatcher\u7c7b\u7528\u4e8e\u63d0\u4f9b\u76d1\u89c6\u6587\u4ef6\u548c\u76ee\u5f55\u66f4\u6539\u7684\u63d2\u53e3\u3002<\/p>\n

QFileSystemWatcher\u901a\u8fc7\u76d1\u63a7\u6307\u5b9a\u8def\u5f84\u7684\u5217\u8868\uff0c\u76d1\u89c6\u6587\u4ef6\u7cfb\u7edf\u4e2d\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u53d8\u66f4\u3002<\/p>\n

\u8c03\u7528addPath()\u51fd\u6570\u53ef\u4ee5\u76d1\u63a7\u4e00\u4e2a\u7279\u5b9a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u3002\u5047\u5982\u987b\u8981\u76d1\u63a7\u591a\u4e2a\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528addPaths()\u3002\u901a\u8fc7\u4f7f\u7528removePath()\u548cremovePaths()\u51fd\u6570\u6765\u79fb\u9664\u73b0\u6709\u8def\u5f84\u3002<\/p>\n

QFileSystemWatcher\u68c0\u6d4b\u6dfb\u52a0\u5230\u5b83\u7684\u6bcf\u4f4d\u8def\u5f84\uff0c\u5df2\u6dfb\u52a0\u5230QFileSystemWatcher\u7684\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u7684files()\u51fd\u6570\u8fdb\u884c\u8bbf\u95ee\uff0c\u76ee\u5f55\u5219\u4f7f\u7528directories()\u51fd\u6570\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n

\u5f53\u4e00\u4e2a\u6587\u4ef6\u88ab\u66f4\u6539\u3001\u91cd\u547d\u540d\u6216\u4ecec\u76d8\u4e0a\u5220\u6389\u65f6\uff0c\u4f1a\u53d1\u51fafileChanged()\u8baf\u53f7\u3002\u540c\u6837\uff0c\u5f53\u4e00\u4e2a\u76ee\u5f55\u6216\u5b83\u7684\u5185\u5bb9\u88ab\u66f4\u6539\u6216\u5220\u6389\u65f6\uff0c\u4f1a\u53d1\u5c04directoryChanged()\u8baf\u53f7\u3002\u987b\u8981\u6ce8\u610f\uff1a\u6587\u4ef6\u4e00\u65e6\u88ab\u91cd\u547d\u540d\u6216\u4ece\u786c\u789f\u5220\u6389\uff0c\u76ee\u5f55\u4e00\u65e6\u4ecec\u76d8\u4e0a\u5220\u6389\uff0cQFileSystemWatcher\u5c06\u505c\u6b62\u76d1\u63a7\u3002<\/p>\n

\u6ce8\uff1a\u76d1\u63a7\u6587\u4ef6\u548c\u76ee\u5f55\u8fdb\u884c\u66f4\u6539\u7684\u884c\u4e3a\u4f1a\u6d88\u8017\u7cfb\u7edf\u8d44\u6e90\u3002\u8fd9\u610f\u5473\u7740\uff0c\u4f60\u7684\u8fdb\u7a0b\u540c\u65f6\u76d1\u63a7\u4f1a\u6709\u6587\u4ef6\u6570\u76ee\u7684\u9650\u5236\u3002\u4e00\u4e9b\u7cfb\u7edf\u9650\u5236\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6570\u76ee\u9ed8\u8ba4\u4e3a256\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5047\u5982\u4f60\u7684\u8fdb\u7a0b\u8bd5\u4f7f\u7528addPath()\u548caddPaths()\u51fd\u6570\u6dfb\u52a0\u8d85\u8fc7256\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u5230\u6587\u4ef6\u7cfb\u7edf\u5c06\u4f1a\u5931\u8d25\u3002<\/p>\n

6.2\u516c\u6709\u51fd\u6570<\/p>\n

\n

QFileSystemWatcher(const QStringList &paths, QObject *parent = nullptr)\nQFileSystemWatcher(QObject *parent = nullptr)<\/code><\/pre>\n<\/p>\n

\n

bool  addPath(const QString &path)\nQStringList addPaths(const QStringList &paths)<\/code><\/pre>\n<\/p>\n

\n

QStringList directories() const\nQStringList files() const<\/code><\/pre>\n<\/p>\n

\n

bool removePath(const QString &path)\nQStringList removePaths(const QStringList &paths)<\/code><\/pre>\n<\/p>\n

6.3\u8baf\u53f7<\/p>\n

\n

void directoryChanged(const QString &path)\nvoid fileChanged(const QString &path)<\/code><\/pre>\n<\/p>\n

\u793a\u4f8b:<\/p>\n

\u6765\u5b9e\u73b0\u4e00\u4e2a\u6587\u4ef6\/\u76ee\u5f55\u76d1\u63a7\u7c7b\u578b\u3002<\/p>\n

\n

#include \n#include \n#include \n#include \n#include \n

<\/p>\n#include \nclass FileWatcher : public QObject\n{\n Q_OBJECT\npublic:\n FileWatcher():mFileWatcher(new QFileSystemWatcher(this))\n {\n connect(mFileWatcher, &QFileSystemWatcher::directoryChanged, this, &FileWatcher::onDirChanged);\n connect(mFileWatcher, &QFileSystemWatcher::fileChanged, this, &FileWatcher::onFileChanged);\n };\n void addWatcherPath(const QString&path)\n {\n \/\/\u628a\u6bcf\u4e2a\u76ee\u5f55\u548c\u6587\u4ef6\u90fd\u6dfb\u52a0\u76d1\u89c6\n mFileWatcher->addPath(path);\n \/\/\u5982\u679c\u76d1\u89c6\u7684\u662f\u76ee\u5f55\uff0c\u5219\u628a\u76ee\u5f55\u4e2d\u7684\u9879\u4fdd\u5b58\u8d77\u6765\n if(QFileInfo(path).isDir())\n mCurContentsMap.insert(path,QDir(path).entryList(QDir::Filter::NoDotAndDotDot | QDir::Dirs | QDir::Files));\n\u200b\n \/\/\u904d\u5386\u5b50\u9879\n QDir dir(path);\n for(QString ph : dir.entryList(QDir::Filter::NoDotAndDotDot | QDir::Dirs | QDir::Files))\n {\n if(QFileInfo(ph).isFile())\n {\n mFileWatcher->addPath(ph);\n }\n else \/\/\u76ee\u5f55\n {\n addWatcherPath(ph);\n }\n }\n }\n\u200b\npublic slots:\n void onDirChanged(const QString& path)\n {\n qDebug() << path << "dirChanged"<directories();\n \/\/\u83b7\u53d6\u76ee\u5f55\u4e0b\u6240\u6709\u9879\u5217\u8868\n auto curEntryList = QDir(path).entryList(QDir::Filter::NoDotAndDotDot | QDir::Dirs | QDir::Files);\n\u200b\n \/\/\u83b7\u53d6\u76ee\u5f55\u4e0b\u539f\u6765\u9879\u7684\u5217\u8868\n auto oldEntryList = mCurContentsMap[path];\n\u200b\n \/\/qDebug()<<"curEntryList"<<curEntryList;\n \/\/qDebug()<<"oldEntryList"<<oldEntryList;\n\u200b\n\u200b\n \/\/\u628acurEntryList\u548coldEntryList\u8f6c\u6210\u96c6\u5408set\n QSet curDirSet(curEntryList.begin(),curEntryList.end());\n QSet oldDirSet(oldEntryList.begin(),oldEntryList.end());\n\u200b\n\u200b\n \/\/\u66f4\u65b0\u8bbe\u7f6e\n mCurContentsMap[path] = curEntryList;\n\u200b\n \/\/\u5224\u65ad\u662f\u5426\u4fee\u6539\u4e86\u6587\u4ef6\uff1a\u5982\u679c\u65b0\u8001\u96c6\u5408\u5927\u5c0f\u76f8\u7b49\uff0c\u8bf4\u660e\u53ea\u662f\u6587\u4ef6\u540d\u6539\u53d8\u4e86\n if(curDirSet.size() == oldDirSet.size())\n {\n auto modifyDir = curDirSet - oldDirSet;\n if(!modifyDir.isEmpty())\n {\n \/\/\u5bf9\u4fee\u6539\u7684\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\n qDebug()<<"\u4fee\u6539"< oldDirSet.size())\n {\n auto addDir = curDirSet - oldDirSet;\n if(!addDir.isEmpty())\n {\n \/\/\u5bf9\u6dfb\u52a0\u7684\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\n qDebug()<<"\u6dfb\u52a0"<<a style='color:#0000CC;' ddDir;\n }\n\u200b\n }\n \/\/\u5224\u65ad\u662f\u5426\u5220\u9664\u4e86\u6587\u4ef6\n else if(curDirSet.size() < oldDirSet.size())\n {\n auto delDir = curDirSet - oldDirSet;\n if(!delDir.isEmpty())\n {\n \/\/\u5bf9\u5220\u9664\u7684\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\n qDebug()<<"\u5220\u9664"<<delDir;\n }\n }\n }\n void onFileChanged(const QString& file)\n {\n qDebug() << file << "fileChanged";\n\u200b\n QFileInfo info(file);\n QString strPath = info.absolutePath();\n QString strName = info.fileName();\n\u200b\n qDebug() << QString("The file %1 at path %2 is updated").arg(strName).arg(strPath);\n }\nprivate:\n QFileSystemWatcher *mFileWatcher;\n QMap mCurContentsMap; \/\/\u6bcf\u4e2a\u76d1\u63a7\u8def\u5f84\u5bf9\u5e94\u7684\u5217\u8868\n};\n\u200b\n\u200b\nint main(int argc, char *argv[])\n{\n QCoreApplication a(argc, argv);\n\u200b\n FileWatcher watcher;\n watcher.addWatcherPath(\".\/\");\n\u200b\n return a.exec();\n}\n\u200b\n#include\"main.moc\"<\/code><\/pre>\n<\/p>\n

7.QStandardPaths7.1\u7b80\u4ecb<\/p>\n

QStandardPaths\u7c7b\u63d0\u4f9b\u4e86\u8bbf\u95ee\u6807\u51c6\u8def\u5f84\u7684\u6280\u5de7\u3002\u6240\u8c13\u7cfb\u7edf\u6807\u51c6\u8def\u5f84\u6307\u7684\u662f\u672c\u5730\u6587\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u7528\u6237\u7684\u7279\u5b9a\u76ee\u5f55\u6216\u7cfb\u7edf\u7684\u914d\u7f6e\u76ee\u5f55\u3002\u4f8b\u5982\u5728Windows\u7cfb\u7edf\u4e2d\u7684\u201c\u6211\u7684\u6587\u6863\u201d\uff0c\u201c\u89c6\u9891\u201d\uff0c\u201c\u56fe\u7247\u201d\u7b49\u76ee\u5f55\u4f4d\u7f6e\u3002<\/p>\n

\u5bf9\u4e8e\u4e00\u4e2a\u5c0f\u578b\u9879\u76ee\uff0c\u7cfb\u7edf\u7684\u6807\u51c6\u76ee\u5f55\u662f\u4fdd\u5b58\u6570\u636e\uff0c\u914d\u7f6e\u4fe1\u606f\u7684\u4e00\u4e2a\u7279\u522b\u6709\u7528\u7684\u5730\u65b9\u3002\u6bd4\u5982\uff0c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u987b\u8981\u5c06\u4e0b\u8f7d\u597d\u7684\u6587\u6863\u4fdd\u5b58\u5728\u672c\u5730\u6587\u4ef6\u7cfb\u7edf\u7684\u67d0\u4e2a\u5730\u65b9\uff0c\u800c\u5b83\u4e0d\u80fd\u5047\u5b9a\u67d0\u4e2a\u5b9a\u597d\u7684\u8def\u5f84\u662f\u5b58\u5728\u4e8ec\u76d8\u4e0a\u7684\u3002\u6709\u7684\u4eba\u53ef\u80fd\u4f1a\u5c06\u8fd9\u4e2a\u6587\u6863\u4fdd\u5b58\u5728\u5e94\u7528\u7a0b\u5e8f\u6240\u5728\u7684\u5de5\u4f5c\u76ee\u5f55\u4e2d\uff0c\u5176\u5b9e\u8fd9\u662f\u4e2a\u597d\u65b9\u5f0f\uff0c\u4f46\u6709\u65f6\u5e94\u7528\u7a0b\u5e8f\u5e76\u4e0d\u5e0c\u671b\u5c06\u6570\u636e\u653e\u5230\u5de5\u4f5c\u76ee\u5f55\u4e2d\uff0c\u8fd9\u662f\u7531\u4e8e\uff1a<\/p>\n

\u5bf9\u4e8e\u7cfb\u7edf\u6807\u51c6\u76ee\u5f55\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba4\u5b9a\u5b83\u662f\u5fc5\u7136\u5b58\u5728\u7684\uff08\u867d\u7136\u4e0d\u5b58\u5728\uff0c\u4e5f\u53ef\u624b\u52a8\u521b\u5efa\uff09\uff0c\u5e76\u4e14\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u53ef\u80fd\u6709\u4e0d\u4e00\u6837\u7684\u7cfb\u7edf\u6807\u51c6\u76ee\u5f55\u3002\u8bf8\u5982\u201c\u6211\u7684\u6587\u6863\u201d\u76ee\u5f55\u4f4d\u7f6e<\/p>\n

\u5047\u5982\u60f3\u8981\u505a\u8de8\u5e73\u53f0\u7684\u7cfb\u7edf\uff0c\u50cf\u8fd9\u79cd\u8def\u5f84\uff0c\u4f60\u90fd\u5f97\u60f3\u529e\u6cd5\u83b7\u53d6\uff0c\u8fd9\u53ea\u662f\u4e00\u4e2a\u6211\u7684\u6587\u6863\uff0c\u5047\u5982\u518d\u52a0\u4e0a\u201c\u4e0b\u8f7d\u201d\uff0c\u201c\u56fe\u7247\u201d\u7b49\u6807\u51c6\u8def\u5f84\uff0c\u60f3\u60f3\u662f\u4e0d\u662f\u90fd\u5f88\u9ebb\u70e6\u3002<\/p>\n

\u4f46\u662f\uff0cQt\u5374\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u5341\u5206\u4fbf\u6377\u7684\u7c7b\u6765\u83b7\u53d6\u8fd9\u79cd\u6807\u51c6\u76ee\u5f55\u8def\u5f84\uff0c\u5b83\u5c31\u662f\u9a6c\u4e0a\u8981\u5b66\u4e60\u7684QStandardPaths\u7c7b\u3002\u6240\u6709\u51fd\u6570\u5747\u4e3a\u9759\u6001\u51fd\u6570\u3002<\/p>\n

7.2\u9759\u6001\u516c\u6709\u51fd\u6570<\/p>\n

(1)\u8fd4\u56de\u7ed9\u5b9a\u4f4d\u7f6e\u7c7b\u578b\u7684\u672c\u5730\u5316\u663e\u793a\u540d\u79f0\uff0c\u5047\u5982\u627e\u4e0d\u5230\u76f8\u5173\u4f4d\u7f6e\uff0c\u5219\u8fd4\u56de\u7a7aQString\u3002<\/p>\n

\u53c2\u6570\u5217\u8868\u4e2dStandardLocationtype\u662f\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u4e0b\u8fb9\u4f1a\u89e3\u91ca\u6240\u6709\u7c7b\u578b<\/p>\n

\n

[static] QString displayName(QStandardPaths::StandardLocation type)<\/code><\/pre>\n<\/p>\n

(2)\u5728\u6307\u5b9a\u8def\u5f84\u4e2d\u67e5\u627e\u540d\u4e3aexecutableName\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5047\u5982\u8def\u5f84\u4e3a\u7a7a\u5219\u67e5\u627e\u7cfb\u7edf\u8def\u5f84\u3002<\/p>\n

\n

[static] QString findExecutable(const QString &executableName, const QStringList &paths = QStringList())<\/code><\/pre>\n<\/p>\n

(3)\u5728type\u7684\u6807\u51c6\u4f4d\u7f6e\u4e2d\u67e5\u627e\u540d\u4e3afileName\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/p>\n

\n

[static] QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)<\/code><\/pre>\n<\/p>\n

(4)\u4f9d\u636e\u6587\u4ef6\u540dfileName\u5728type\u7684\u6807\u51c6\u4f4d\u7f6e\u67e5\u627e\u6240\u6709\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/p>\n

\n

[static] QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)<\/code><\/pre>\n<\/p>\n

(5)\u5047\u5982testMode\u4e3atrue\uff0c\u8fd9\u5c06\u5728QStandardPaths\u4e2d\u542f\u7528\u4e00\u4e2a\u7279\u6b8a\u7684\u201c\u6d4b\u8bd5\u6a21\u5f0f\u201d\u7ea2\u5e3d\u5b50linux\uff0c\u5b83\u5c06\u4fee\u6539\u53ef\u5199\u7684\u4f4d\u7f6e\u4ee5\u6307\u5411\u6d4b\u8bd5\u76ee\u5f55\u3002\u8fd9\u5c06\u5236\u6b62\u624b\u52a8\u6d4b\u8bd5\u8bfb\u53d6\u6216\u5199\u5165\u5f53\u524d\u7528\u6237\u7684\u914d\u7f6e\u3002<\/p>\n

\n

[static] void setTestModeEnabled(bool testMode)<\/code><\/pre>\n<\/p>\n

(6)\u8fd4\u56de\u8be5\u7c7b\u578b\u6587\u4ef6\u6240\u5c5e\u7684\u6240\u6709\u76ee\u5f55\u3002<\/p>\n

\n

[static] QStringList standardLocations(QStandardPaths::StandardLocation type)<\/code><\/pre>\n<\/p>\n

(7)\u8fd4\u56de\u5199\u5165\u7c7b\u578b\u4e3a\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u5018\u82e5\u672a\u80fd\u786e\u5b9a\u4f4d\u7f6e\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n

\n

[static] QString writableLocation(QStandardPaths::StandardLocation type)<\/code><\/pre>\n<\/p>\n

7.3enumQStandardPaths::StandardLocation<\/p>\n

\u679a\u4e3e\u5e38\u91cf<\/p>\n

\u503c<\/p>\n

\u63cf\u8ff0<\/p>\n

QStandardPaths::DesktopLocation<\/p>\n

0<\/p>\n

\u8fd4\u56de\u7528\u6237\u7684\u684c\u9762\u76ee\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u5728\u6ca1\u6709\u684c\u9762\u6982\u5ff5\u7684\u7cfb\u7edf\u4e0a\u3002<\/p>\n

QStandardPaths::DocumentsLocation<\/p>\n

1<\/p>\n

\u8fd4\u56de\u5305\u542b\u7528\u6237\u6587\u6863\u6587\u4ef6\u7684\u76ee\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u8fd4\u56de\u7684\u8def\u5f84\u4ece\u6765\u4e0d\u662f\u7a7a\u7684\u3002<\/p>\n

QStandardPaths::FontsLocation<\/p>\n

2<\/p>\n

\u8fd4\u56de\u5305\u542b\u7528\u6237\u5b57\u4f53\u7684\u76ee\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u6ce8\u610f\uff0c\u5b89\u88c5\u5b57\u4f53\u53ef\u80fd\u987b\u8981\u989d\u5916\u7684\u3001\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u64cd\u4f5c\u3002<\/p>\n

QStandardPaths::ApplicationsLocation<\/p>\n

3<\/p>\n

\u8fd4\u56de\u5305\u542b\u7528\u6237\u5e94\u7528\u7a0b\u5e8f(\u53ef\u6267\u884c\u7a0b\u5e8f\u3001\u5e94\u7528\u7a0b\u5e8f\u5305\u6216\u5b83\u4eec\u7684\u5feb\u6377\u65b9\u6cd5)\u7684\u76ee\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u6ce8\u610f\uff0c\u5b89\u88c5\u5e94\u7528\u7a0b\u5e8f\u53ef\u80fd\u987b\u8981\u989d\u5916\u7684\u3001\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u64cd\u4f5c\u3002\u8be5\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u3001\u6587\u4ef6\u5939\u6216\u5feb\u6377\u65b9\u6cd5\u662f\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u3002<\/p>\n

QStandardPaths::MusicLocation<\/p>\n

4<\/p>\n

\u8fd4\u56de\u5305\u542b\u7528\u6237\u97f3\u4e50\u6216\u5176\u4ed6\u97f3\u9891\u6587\u4ef6\u7684\u76ee\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u82e5\u679c\u4e0d\u5b58\u5728\u4e13\u95e8\u7528\u4e8e\u97f3\u4e50\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u7528\u4e8e\u50a8\u5b58\u7528\u6237\u6587\u6863\u7684\u5408\u7406\u540e\u5907\u65b9\u6848\u3002<\/p>\n

QStandardPaths::MoviesLocation<\/p>\n

5<\/p>\n

\u8fd4\u56de\u5305\u542b\u7528\u6237\u5f71\u7247\u548c\u89c6\u9891\u7684\u76ee\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u7c7b\u5e93\u503c\u3002\u82e5\u679c\u4e0d\u5b58\u5728\u7279\u5b9a\u4e8e\u5f71\u7247\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u5219\u8fd4\u56de\u7528\u4e8e\u50a8\u5b58\u7528\u6237\u6587\u6863\u7684\u5408\u7406\u7684\u5907\u7528\u65b9\u6848\u3002<\/p>\n

QStandardPaths::PicturesLocation<\/p>\n

6<\/p>\n

\u8fd4\u56de\u5305\u542b\u7528\u6237\u56fe\u7247\u6216\u76f8\u7247\u7684\u76ee\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u82e5\u679c\u6ca1\u6709\u7279\u5b9a\u7684\u76ee\u5f55\u5bf9\u4e8e\u65e9\u5df2\u5b58\u5728\u7684\u56fe\u7247\u6587\u4ef6\uff0c\u5c06\u8fd4\u56de\u50a8\u5b58\u7528\u6237\u6587\u6863\u7684\u5408\u7406\u9000\u6b65\u3002<\/p>\n

QStandardPaths::TempLocation<\/p>\n

7<\/p>\n

\u8fd4\u56de\u53ef\u4ee5\u50a8\u5b58\u4e34\u65f6\u6587\u4ef6\u7684\u76ee\u5f55\u3002\u8fd4\u56de\u503c\u53ef\u80fd\u662f\u7279\u5b9a\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\uff0c\u5728\u6b64\u7528\u6237\u7684\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u5171\u4eab\uff0c\u751a\u81f3\u5728\u7cfb\u7edf\u8303\u56f4\u5185\u5171\u4eab\u3002\u8fd4\u56de\u7684\u8def\u5f84\u4ece\u6765\u4e0d\u662f\u7a7a\u7684\u3002<\/p>\n

QStandardPaths::HomeLocation<\/p>\n

8<\/p>\n

\u8fd4\u56de\u7528\u6237\u7684\u4e3b\u76ee\u5f55(\u4e0eQDir::homePath()\u76f8\u540c)\u3002\u5728Unix\u7cfb\u7edf\u4e0a\uff0c\u8fd9\u7b49\u4e8eHOME\u73af\u5883\u53d8\u91cf\u3002\u8fd9\u4e2a\u503c\u53ef\u4ee5\u662f\u901a\u7528\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u7279\u5b9a\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\uff0c\u5e76\u4e14\u8fd4\u56de\u7684\u8def\u5f84\u4ece\u6765\u4e0d\u662f\u7a7a\u7684\u3002<\/p>\n

QStandardPaths::DataLocation<\/p>\n

9<\/p>\n

\u8fd4\u56de\u4e0eAppLocalDataLocation\u76f8\u540c\u7684\u503c\u3002\u6b64\u679a\u4e3e\u503c\u5df2\u5f03\u7528\u3002\u4f7f\u7528AppDataLocation\u66f4\u53ef\u53d6\uff0c\u7531\u4e8e\u5728Windows\u4e0a\uff0c\u63a8\u8350\u4f7f\u7528\u6f2b\u6e38\u8def\u5f84\u3002<\/p>\n

QStandardPaths::CacheLocation<\/p>\n

10<\/p>\n

\u8fd4\u56de\u5e94\u5f53\u5199\u5165\u7528\u6237\u7279\u5b9a\u7684\u975e\u5fc5\u8981(\u7f13\u5b58)\u6570\u636e\u7684\u76ee\u5f55\u4f4d\u7f6e\u3002\u8fd9\u662f\u4e00\u4e2a\u7279\u5b9a\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u76ee\u5f55\u3002\u8fd4\u56de\u7684\u8def\u5f84\u4ece\u6765\u4e0d\u662f\u7a7a\u7684\u3002<\/p>\n

QStandardPaths::GenericCacheLocation<\/p>\n

15<\/p>\n

\u8fd4\u56de\u5e94\u5199\u5165\u8de8\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\u7684\u7279\u5b9a\u4e8e\u7528\u6237\u7684\u975e\u5fc5\u8981(\u7f13\u5b58)\u6570\u636e\u7684\u76ee\u5f55\u4f4d\u7f6e\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u6ce8\u610f\uff0c\u5047\u82e5\u7cfb\u7edf\u6ca1\u6709\u5171\u4eab\u7f13\u5b58\u7684\u6982\u5ff5\uff0c\u5219\u8fd4\u56de\u7684\u8def\u5f84\u53ef\u80fd\u4e3a\u7a7a\u3002<\/p>\n

QStandardPaths::GenericDataLocation<\/p>\n

11<\/p>\n

\u8fd4\u56de\u53ef\u50a8\u5b58\u8de8\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\u7684\u6301\u4e45\u6570\u636e\u7684\u76ee\u5f55\u4f4d\u7f6e\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u8fd4\u56de\u7684\u8def\u5f84\u4ece\u6765\u4e0d\u662f\u7a7a\u7684\u3002<\/p>\n

QStandardPaths::RuntimeLocation<\/p>\n

12<\/p>\n

\u8fd4\u56de\u5e94\u5f53\u5199\u5165\u8fd0\u884c\u65f6\u901a\u8baf\u6587\u4ef6\u7684\u76ee\u5f55\u4f4d\u7f6e\uff0c\u5982Unix\u672c\u5730\u5957\u63a5\u5b57\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u5728\u4e2a\u522b\u7cfb\u7edf\u4e0a\uff0c\u8fd4\u56de\u7684\u8def\u5f84\u53ef\u80fd\u4e3a\u7a7a\u3002<\/p>\n

QStandardPaths::ConfigLocation<\/p>\n

13<\/p>\n

\u8fd4\u56de\u5e94\u5f53\u5199\u5165\u7528\u6237\u7279\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u76ee\u5f55\u4f4d\u7f6e\u3002\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u901a\u7528\u503c\u6216\u7279\u5b9a\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u503c\uff0c\u4f46\u662f\u8fd4\u56de\u7684\u8def\u5f84\u6c38\u8fdc\u4e0d\u4f1a\u4e3a\u7a7a\u3002<\/p>\n

QStandardPaths::DownloadLocation<\/p>\n

14<\/p>\n

\u8fd4\u56de\u7528\u6237\u4e0b\u8f7d\u6587\u4ef6\u7684\u76ee\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\u3002\u5047\u82e5\u4e0d\u5b58\u5728\u4e13\u95e8\u7528\u4e8e\u4e0b\u8f7d\u7684\u76ee\u5f55\uff0c\u5219\u8fd4\u56de\u7528\u4e8e\u50a8\u5b58\u7528\u6237\u6587\u6863\u7684\u5408\u7406\u540e\u5907\u65b9\u6848\u3002<\/p>\n

QStandardPaths::GenericConfigLocation<\/p>\n

16<\/p>\n

\u8fd4\u56de\u5e94\u5f53\u5199\u5165\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u5171\u4eab\u7684\u7528\u6237\u7279\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u76ee\u5f55\u4f4d\u7f6e\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u7c7b\u503c\uff0c\u8fd4\u56de\u7684\u8def\u5f84\u4ece\u4e0d\u4e3a\u7a7a\u3002<\/p>\n

QStandardPaths::AppDataLocation<\/p>\n

17<\/p>\n

\u8fd4\u56de\u53ef\u50a8\u5b58\u6301\u4e45\u5e94\u7528\u7a0b\u5e8f\u6570\u636e\u7684\u76ee\u5f55\u4f4d\u7f6e\u3002\u8fd9\u662f\u4e00\u4e2a\u7279\u5b9a\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u76ee\u5f55\u3002\u8981\u83b7\u53d6\u50a8\u5b58\u8981\u4e0e\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\u7684\u6570\u636e\u7684\u8def\u5f84\uff0c\u8bf7\u4f7f\u7528QStandardPaths::GenericDataLocation\u3002\u8fd4\u56de\u7684\u8def\u5f84\u4ece\u6765\u4e0d\u662f\u7a7a\u7684\u3002\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u8fd9\u5c06\u8fd4\u56de\u6f2b\u6e38\u8def\u5f84\u3002\u8fd9\u4e2aenum\u503c\u662f\u5728Qt5.4\u4e2d\u6dfb\u52a0\u7684\u3002<\/p>\n

QStandardPaths::AppLocalDataLocation<\/p>\n

DataLocation<\/p>\n

\u8fd4\u56deWindows\u64cd\u4f5c\u7cfb\u7edf\u7684\u672c\u5730\u8bbe\u7f6e\u8def\u5f84\u3002\u5728\u6240\u6709\u5176\u4ed6\u5e73\u53f0\u4e0a\uff0c\u5b83\u8fd4\u56de\u4e0eAppDataLocation\u76f8\u540c\u7684\u503c\u3002\u8fd9\u4e2aenum\u503c\u662f\u5728Qt5.4\u4e2d\u6dfb\u52a0\u7684\u3002<\/p>\n

QStandardPaths::AppConfigLocation<\/p>\n

18<\/p>\n

\u8fd4\u56de\u5e94\u5f53\u5199\u5165\u7528\u6237\u7279\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u76ee\u5f55\u4f4d\u7f6e\u3002\u8fd9\u662f\u4e00\u4e2a\u7279\u5b9a\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u76ee\u5f55\uff0c\u8fd4\u56de\u7684\u8def\u5f84\u6c38\u8fdc\u4e0d\u4f1a\u4e3a\u7a7a\u3002\u8fd9\u4e2aenum\u503c\u662f\u5728Qt5.5\u4e2d\u6dfb\u52a0\u7684\u3002<\/p>\n

7.4\u4f7f\u7528\u65b9\u5f0f<\/p>\n

\n

qDebug() << "DesktopLocation: " << QStandardPaths::displayName(QStandardPaths::DesktopLocation);\nqDebug() << "DocumentsLocation: " << QStandardPaths::displayName(QStandardPaths::DocumentsLocation);\nqDebug() << "FontsLocation: " << QStandardPaths::displayName(QStandardPaths::FontsLocation);\nqDebug() << "ApplicationsLocation: " << QStandardPaths::displayName(QStandardPaths::ApplicationsLocation);\nqDebug() << "MusicLocation: " << QStandardPaths::displayName(QStandardPaths::MusicLocation);\nqDebug() << "MoviesLocation: " << QStandardPaths::displayName(QStandardPaths::MoviesLocation);\nqDebug() << "PicturesLocation: " << QStandardPaths::displayName(QStandardPaths::PicturesLocation);\nqDebug() << "TempLocation: " << QStandardPaths::displayName(QStandardPaths::TempLocation);\nqDebug() << "HomeLocation: " << QStandardPaths::displayName(QStandardPaths::HomeLocation);\nqDebug() << "DataLocation: " << QStandardPaths::displayName(QStandardPaths::DataLocation);\nqDebug() << "CacheLocation: " << QStandardPaths::displayName(QStandardPaths::CacheLocation);\nqDebug() << "GenericCacheLocation: " << QStandardPaths::displayName(QStandardPaths::GenericCacheLocation);\nqDebug() << "GenericDataLocation: " << QStandardPaths::displayName(QStandardPaths::GenericDataLocation);\nqDebug() << "RuntimeLocation: " << QStandardPaths::displayName(QStandardPaths::RuntimeLocation);\nqDebug() << "ConfigLocation: " << QStandardPaths::displayName(QStandardPaths::ConfigLocation);\nqDebug() << "DownloadLocation: " << QStandardPaths::displayName(QStandardPaths::DownloadLocation);\nqDebug() << "GenericConfigLocation: " << QStandardPaths::displayName(QStandardPaths::GenericConfigLocation);\nqDebug() << "AppDataLocation: " << QStandardPaths::displayName(QStandardPaths::AppDataLocation);\nqDebug() << "AppLocalDataLocation: " << QStandardPaths::displayName(QStandardPaths::AppLocalDataLocation);\nqDebug() << "AppConfigLocation: " << QStandardPaths::displayName(QStandardPaths::AppConfigLocation);<\/code><\/pre>\n<\/p>\n

\n

qDebug() << QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);\nqDebug() << QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);<\/code><\/pre>\n<\/p>\n

7.5\u5728\u6307\u5b9a\u4f4d\u7f6e\u67e5\u627e\u6587\u4ef6<\/p>\n

\n

qDebug()<<QStandardPaths::findExecutable("calc.exe");\t\/\/\u53ea\u8981\u8bbe\u7f6e\u4e86path\u73af\u5883\u53d8\u91cf\uff0c\u90fd\u53ef\u4ee5\u627e\u5230\nqDebug()<<QStandardPaths::findExecutable("7z.exe",QStringList()<<"D:\\MySoftWare\\7-Zip");\t\/\/\u5982\u679c\u6ca1\u6709\u8bbe\u7f6epath\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u81ea\u5df1\u6307\u5b9a\u591a\u4e2a\u8def\u5f84<\/code><\/pre>\n<\/p>\n

\n

qDebug()<< QStandardPaths::locate(QStandardPaths::StandardLocation::DownloadLocation,"\u4e0b\u62c9.png");<\/code><\/pre>\n<\/p>\n

8.QSettings8.1\u7b80\u4ecb<\/p>\n

\u7528\u6237\u4e00\u822c\u5e0c\u671b\u5e94\u7528\u7a0b\u5e8f\u5728\u4f1a\u8bdd\u4e2d\u8bb0\u4f4f\u5b83\u7684\u8bbe\u7f6e(\u7a97\u53e3\u5927\u5c0f\u548c\u4f4d\u7f6e\uff0c\u9009\u9879\u7b49)\u3002\u8fd9\u79cd\u4fe1\u606f\u4e00\u822c\u50a8\u5b58\u5728Windows\u4e0a\u7684\u7cfb\u7edf\u6ce8\u518c\u8868\u4e2d(HKEY_CURRENT_USERSoftware\/MySoft)\uff0c\u4ee5\u53camacOS\u548ciOS\u4e0a\u7684\u5c5e\u6027\u5217\u8868\u6587\u4ef6\u4e2d\u3002\u5728Unix\u7cfb\u7edf\u4e0a\uff0c\u5728\u6b20\u7f3a\u6807\u51c6\u7684\u60c5\u51b5\u4e0b\uff0c\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f(\u5305\u62ecKDE\u5e94\u7528\u7a0b\u5e8f)\u4f7f\u7528INI\u6587\u672c\u6587\u4ef6\u3002<\/p>\n

QSettings\u662f\u5bf9\u8fd9\u79cd\u6280\u672f\u7684\u5177\u8c61\uff0c\u4f7f\u60a8\u8fd8\u80fd\u4ee5\u53ef\u79fb\u690d\u7684\u65b9\u6cd5\u4fdd\u5b58\u548c\u6062\u590d\u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e\u3002\u5b83\u8fd8\u652f\u6301\u81ea\u5b9a\u4e49\u50a8\u5b58\u683c\u5f0f\u3002<\/p>\n

QSettings\u7684API\u57fa\u4e8eQVariant\uff0c\u56e0\u800c\u6211\u4eec\u53ef\u4ee5\u4fdd\u5b58\u597d\u591a\u7684\u7c7b\u578b\uff0c\u5982QString\u3001QRect\u548cQImage\u3002<\/p>\n

\u5018\u82e5\u60a8\u6240\u987b\u8981\u7684\u53ea\u662f\u4e00\u4e2a\u975e\u6301\u4e45\u7684\u57fa\u4e8e\u663e\u5b58\u7684\u7ed3\u6784\uff0c\u8fd9\u4e48\u53ef\u4ee5\u8003\u8651\u4f7f\u7528QMap\u4ee3\u66ff\u3002<\/p>\n

8.2\u57fa\u672c\u7528\u6cd5<\/p>\n

\u521b\u5efaQSettings\u5bf9\u8c61\u65f6\uff0c\u5fc5\u987b\u4f20\u9012\u516c\u53f8\u6216\u7ec4\u7ec7\u7684\u540d\u79f0\u4ee5\u53ca\u5e94\u7528\u7a0b\u5e8f\u7684\u540d\u79f0\u3002\u8bf8\u5982\uff0c\u5047\u5982\u4f60\u7684\u7a0b\u5e8f\u540d\u4e3aQQ\uff0c\u800c\u4f60\u7684\u516c\u53f8\u540d\u4e3aNiuBi\uff0c\u4f60\u5c06\u6784\u9020QSettings\u5bf9\u8c61\u5982\u4e0b:<\/p>\n

\n

QSettings setting(\"NiuBi\",\"QQ\");<\/code><\/pre>\n<\/p>\n

QSettings\u5bf9\u8c61\u65e2\u53ef\u4ee5\u5728\u5806\u6808\u4e0a\u521b\u5efa\uff0c\u4e5f\u53ef\u4ee5\u5728\u5806\u4e0a\u521b\u5efa(\u5373\u4f7f\u7528new)\u3002\u6784\u9020\u548c\u9500\u6bc1QSettings\u5bf9\u8c61\u7279\u522b\u5feb\u3002<\/p>\n

\u5047\u5982\u4f60\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u597d\u591a\u5730\u65b9\u4f7f\u7528QSettings\uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u4f7f\u7528QCoreApplication::setOrganizationName()\u548cQCoreApplication::setApplicationName()\u6307\u5b9a\u7ec4\u7ec7\u540d\u79f0\u548c\u5e94\u7528\u7a0b\u5e8f\u540d\u79f0\uff0c\u4e4b\u540e\u4f7f\u7528\u9ed8\u8ba4\u7684QSettings\u6784\u9020\u51fd\u6570:<\/p>\n

\n

QCoreApplication::setOrganizationName(\"NiuBi\");\nQCoreApplication::setOrganizationDomain(\"NiuBi.com\");\nQCoreApplication::setApplicationName(\"QQ\");\n...\nQSettings settings;<\/code><\/pre>\n<\/p>\n

QSettings\u50a8\u5b58\u8bbe\u7f6e\u3002\u6bcf\u4f4d\u8bbe\u7f6e\u7531\u4e00\u5bf9(key,value)\u901a\u914d\u7b26\u5bf9(key\u4e3aQStirng\u7c7b\u578b\uff0cvalue\u4e3aQVariant)\u7ec4\u6210\u3002\u8981\u5199\u5165\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528setValue()\u3002\u6bd4\u5982:<\/p>\n

\n

setting.setValue(\"size\",QSize(640,480));\nqDebug()<< setting.fileName();  \/\/\u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u4fdd\u5b58\u4f4d\u7f6e<\/code><\/pre>\n<\/p>\n

\u5047\u5982\u65e9\u5df2\u5b58\u5728\u5177\u6709\u76f8\u540c\u952e\u7684\u8bbe\u7f6e\uff0c\u5219\u73b0\u6709\u503c\u5c06\u88ab\u65b0\u503c\u8986\u76d6\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u4fee\u6539\u53ef\u80fd\u4e0d\u4f1a\u7acb\u523b\u4fdd\u5b58\u5230\u6c38\u4e45\u50a8\u5b58\u4e2d\u3002(\u53ef\u4ee5\u8c03\u7528sync()\u6765\u7acb\u523b\u9012\u4ea4\u4fee\u6539\u3002)<\/p>\n

\u4f60\u53ef\u4ee5\u4f7f\u7528value()\u83b7\u53d6\u8bbe\u7f6e\u7684\u503c:<\/p>\n

\n

QSize size =  setting.value(\"size\",QSize(250,250)).value();<\/code><\/pre>\n<\/p>\n

\u82e5\u679c\u6ca1\u6709\u6307\u5b9a\u540d\u79f0\u7684\u8bbe\u7f6e\uff0cQSettings\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684QVariant(\u65e0\u6548\u7684)\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7ed9value()\u6765\u6307\u5b9a\u53e6\u4e00\u4e2a\u9ed8\u8ba4\u503c(\u8fd9\u513f\u4f20\u4e86QSize(250,250)\uff0c\u5047\u5982\u914d\u7f6e\u6587\u4ef6\u4e2d\u6ca1\u6709size\uff0c\u5c31\u8fd4\u56de\u81ea\u5df1\u4f20\u7684\u8fd9\u4e2a\u6570\u636e)\u3002<\/p>\n

\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f<\/p>\n

\u5728windows\u4e0b\uff0c\u9ed8\u8ba4\u4e3a\u5199\u5165\u6ce8\u518c\u8868\uff0c\u5047\u5982\u60f3\u8981\u5199\u5165\u5230.ini\u6587\u4ef6\u5e76\u4fdd\u5b58\u5230exe\u6240\u5728\u76ee\u5f55\uff0c\u8be5\u5982\u4f55\u8bbe\u7f6e\u5462\uff1f<\/p>\n

\n

QApplication::setOrganizationName(\"NiuBi\");\nQApplication::setApplicationName(\"QQ\");\n\u200b\nQSettings setting(QApplication::applicationDirPath()+\"\/qfile.ini\" ,QSettings::Format::IniFormat);<\/code><\/pre>\n<\/p>\n

8.3\u516c\u6709\u51fd\u6570\u5e38\u7528<\/p>\n

\n

void setValue(const QString &key, const QVariant &value)<\/code><\/pre>\n<\/p>\n

\n

QVariant QSettings::value(const QString &key, const QVariant &defaultValue = QVariant()) const<\/code><\/pre>\n<\/p>\n

\n

void QSettings::sync()<\/code><\/pre>\n<\/p>\n

\n

QSettings::Status status() const<\/code><\/pre>\n<\/p>\n

\n

void remove(const QString &key)<\/code><\/pre>\n<\/p>\n

\n

QString QSettings::fileName() const<\/code><\/pre>\n<\/p>\n

\u5206\u7ec4<\/p>\n

\n

void beginGroup(const QString &prefix)<\/code><\/pre>\n<\/p>\n

\n

void endGroup()<\/code><\/pre>\n<\/p>\n

\n

QString QSettings::group() const<\/code><\/pre>\n<\/p>\n

\n

int beginReadArray(const QString &prefix)<\/code><\/pre>\n<\/p>\n

\n

void beginWriteArray(const QString &prefix, int size = -1)<\/code><\/pre>\n<\/p>\n

\n

void endArray()<\/code><\/pre>\n<\/p>\n

\n

void setArrayIndex(int i)<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"

\u7a0b\u5e8f\u5185\u5d4c\u7684\u8d44\u6e90\u6587\u4ef6\u4e5f\u662f\u901a\u7528\u7684\uff0c\u533a\u522b\u53ea\u662f\u5185\u5d4c\u8d44\u6e90\u6587\u4ef6\u5168\u662f\u53ea\u8bfb\u7684\u3002\u6587\u4ef6\u540d\u7f16\u7801\u79f0\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u672c\u5730\u5316\u7684\u8def\u5f84\u6587\u4ef6\u540d\u3002\u5bf9\u4e8e\u6587\u4ef6\u8bbe\u5907\uff0c\u8fd8\u6709\u4e2a\u4e0d\u5e38\u7528\u7684\u6784\u9020\u51fd\u6570\uff1a\u9664\u4e86\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u6307\u5b9a\u6587\u4ef6\u8bbe\u5907\u6216\u5185\u5b58\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u4fee\u6539\uff1aQFileSystemWatcher\u7c7b\u7528\u4e8e\u63d0\u4f9b\u76d1\u89c6\u6587\u4ef6\u548c\u76ee\u5f55\u4fee\u6539\u7684\u63a5\u53e3\u3002\u8fd4\u56de\u5e94\u8be5\u5199\u5165\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u5171\u4eab\u7684\u7528\u6237\u7279\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u76ee\u5f55\u4f4d\u7f6e\u3002<\/p>\n","protected":false},"author":1,"featured_media":266298,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-266297","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/266297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=266297"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/266297\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/266298"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=266297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=266297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=266297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}