{"id":2637,"date":"2011-02-27T10:46:13","date_gmt":"2011-02-27T02:46:13","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2637"},"modified":"2011-03-01T21:04:43","modified_gmt":"2011-03-01T13:04:43","slug":"%e5%9f%ba%e4%ba%8eqt%e7%9a%84http%e7%bc%96%e7%a8%8b-%e8%bf%9b%e9%98%b6","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2637","title":{"rendered":"\u57fa\u4e8eQt\u7684Http\u7f16\u7a0b-\u8fdb\u9636"},"content":{"rendered":"<h2>\u8fdb\u9636<\/h2>\n<p>\u4e0a\u6587\u7684Http\u5ba2\u6237\u7aef\u53ea\u80fd\u4e0b\u8f7d\u6307\u5b9a\u7f51\u5740\u7684\u6570\u636e\uff0c\u8fd9\u6837\u7684\u5ba2\u6237\u7aef\u5728\u4ea4\u4e92\u6027\u548c\u529f\u80fd\u6027\u4e0a\u90fd\u5f88\u5dee\u3002\u672c\u6587\u6240\u63cf\u8ff0\u7684\u7a0b\u5e8f\u5219\u5728\u8fd9\u4e2a\u57fa\u672c\u7684\u5ba2\u6237\u7aef\u4e0a\u8fdb\u884c\u6539\u9020\uff0c\u5b9e\u73b0\u4efb\u610f\u76ee\u6807\u5730\u5740\u7684\u6570\u636e\u4e0b\u8f7d\uff0c\u5e76\u4e14\u6539\u5584\u4e86\u7528\u6237\u754c\u9762\u7684\u3002\u5177\u4f53UI\u53ef\u53c2\u8003\u4e0b\u56fe\uff1a<\/p>\n<p><a rel=\"attachment wp-att-2629\" href=\"http:\/\/edsionte.com\/techblog\/archives\/2611\/screenshot-widget-2\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2629\" title=\"Screenshot-widget\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/02\/Screenshot-widget-300x154.png\" alt=\"\" width=\"300\" height=\"154\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/02\/Screenshot-widget-300x154.png 300w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/02\/Screenshot-widget.png 346w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u5bf9\u4e8eUI\u800c\u8a00\uff0c\u8be5\u5ba2\u6237\u7aef\u589e\u52a0\u4e86\u4efb\u610f\u5730\u5740\u7684\u8f93\u5165\u6846\u3001\u4e0b\u8f7d\u8fdb\u5ea6\u6761\u548c\u4e0b\u8f7d\u6309\u94ae\uff1b\u5bf9\u4e8e\u4e0b\u8f7d\u7684\u6570\u636e\u800c\u8a00\uff0c\u8be5\u5ba2\u6237\u7aef\u4e0d\u518d\u5c40\u9650\u4e8e\u4e0b\u8f7d\u6587\u672c\u6570\u636e\u3002\u63a5\u4e0b\u6765\u672c\u6587\u5c06\u6309\u7167\u7a0b\u5e8f\u6267\u884c\u7684\u5927\u81f4\u987a\u5e8f\u5bf9\u76f8\u5173\u51fd\u6570\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<h3>\u6784\u9020\u51fd\u6570<\/h3>\n<p>\u7528\u6237\u754c\u9762\u7684\u8bbe\u8ba1\u53ef\u89c1\u672c\u6587\u53c2\u80031\u548c\u53c2\u80032\uff0c\u8fd9\u91cc\u4ece\u6784\u9020\u51fd\u6570\u5f00\u59cb\u3002\u5728\u6784\u9020\u51fd\u6570\u4e2d\u9664\u4e86\u5bf9\u7528\u6237\u754c\u9762\u8fdb\u884c\u521d\u59cb\u5316\u5916\uff0c\u8fd8\u521b\u5efa\u4e86\u4e00\u4e2aQNetworkAccessManager\u5bf9\u8c61\uff0c\u5e76\u5c06\u8fdb\u5ea6\u663e\u793a\u6761\u9690\u85cf\u4e86\u8d77\u6765\u3002<\/p>\n<pre class=\"brush:c\">widget::widget(QWidget *parent) :\r\n    QWidget(parent),\r\n    ui(new Ui::widget)\r\n{\r\n    ui-&gt;setupUi(this);\r\n    manager = new QNetworkAccessManager(this);\r\n    ui-&gt;progressBar-&gt;hide();\r\n}<\/pre>\n<h3>on_pushButton_clicked\u69fd\u51fd\u6570<\/h3>\n<p>\u8be5\u69fd\u51fd\u6570\u5bf9\u5e94\u7740\u4e0b\u8f7d\u6309\u94ae\u7684\u5355\u51fb\u4e8b\u4ef6\u3002\u8be5\u51fd\u6570\u5b8c\u6210\u7684\u5de5\u4f5c\u6709\u4ece\u7528\u6237\u754c\u9762\u83b7\u5f97\u4e0b\u8f7d\u5730\u5740\uff1b\u4ece\u8fd9\u4e2a\u5730\u5740\u4e2d\u89e3\u6790\u51fa\u6240\u4e0b\u8f7d\u6587\u4ef6\u7684\u540d\u79f0\uff1b\u6253\u5f00\uff08\u521b\u5efa\uff09\u6240\u4e0b\u8f7d\u7684\u6587\u4ef6\uff1b\u53d1\u9001\u4e0b\u8f7d\u8bf7\u6c42\uff1b\u66f4\u65b0\u8fdb\u5ea6\u663e\u793a\u6761\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u4ece\u4e0a\u6587\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u5728Qt\u4e2d\u4f7f\u7528QUrl\u7c7b\u6765\u5b58\u50a8url\u5730\u5740\uff0c\u901a\u8fc7\u8be5\u7c7b\u7684\u6210\u5458\u51fd\u6570\u8fd8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u5bf9url\u5730\u5740\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u4f7f\u7528QFileInfo\u7c7b\u7684\u5bf9\u8c61\u5b58\u50a8\u4e0d\u4f9d\u8d56\u5177\u4f53\u7cfb\u7edf\u7684\u6587\u4ef6\u5c5e\u6027\uff0c\u6bd4\u5982\u6587\u4ef6\u7684\u540d\u79f0\uff0c\u8def\u5f84\uff0c\u8bbf\u95ee\u6743\u9650\u7b49\uff1b\u901a\u8fc7\u8be5\u7c7b\u7684\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u7684\u83b7\u53d6\u6587\u4ef6\u7684\u67d0\u4e9b\u5c5e\u6027\uff0c\u6bd4\u5982\u901a\u8fc7fileName\u6210\u5458\u51fd\u6570\u5c31\u53ef\u4ee5\u4ece\u8def\u5f84\u540d\u4e2d\u5feb\u901f\u89e3\u6790\u51fa\u6587\u4ef6\u540d\u3002<\/p>\n<p>\u5f97\u5230\u4e86\u6587\u4ef6\u540d\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aQFile\u7c7b\u7684\u5bf9\u8c61\uff0c\u901a\u8fc7open\u6210\u5458\u51fd\u6570\u5c31\u53ef\u4ee5\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\uff0cQIODevice::WriteOnly\u4e3a\u6253\u5f00\u6a21\u5f0f\u3002\u5982\u679c\u6253\u5f00\u9519\u8bef\uff0c\u5219\u5f39\u51fa\u8b66\u544a\u63d0\u793a\u6846\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n<p>\u6587\u4ef6\u6253\u5f00\u6210\u529f\u540e\uff0c\u7d27\u63a5\u7740\u5c31\u5e94\u8be5\u53d1\u9001\u4e0b\u8f7d\u94fe\u63a5\u7684\u8bf7\u6c42\u4e86\uff0c\u8fd9\u4e2a\u5de5\u4f5c\u5c06\u5728startRequest()\u4e2d\u5b8c\u6210\u3002\u6700\u540e\u8bbe\u7f6e\u8fdb\u5ea6\u66f4\u65b0\u6761\u7684\u521d\u59cb\u503c\uff0c\u5e76\u5c06\u5176\u5728\u754c\u9762\u4e0a\u663e\u793a\u51fa\u6765\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u4ee3\u7801\u53ef\u53c2\u8003\u4e0b\u56fe\uff1a<\/p>\n<pre class=\"brush:c\">void widget::on_pushButton_clicked()\r\n{\r\n    url.setUrl(ui-&gt;lineEdit-&gt;text());\r\n    QFileInfo info(url.path());\/\/\u83b7\u5f97\u5730\u5740\uff1b\r\n    QString fileName(info.fileName());\/\/\u4ece\u5730\u5740\u4e2d\u83b7\u5f97\u6587\u4ef6\u540d\uff1b\r\n\r\n    \/\/\u5982\u679c\u5730\u5740\u7c7b\u4f3cwww.edsionte.com\uff0c\u5219\u6587\u4ef6\u540d\u4e3aindex.html\uff1b\r\n    if (fileName.isEmpty())\r\n        fileName = \"index.html\";\r\n\r\n    file = new QFile(fileName);\r\n    if (!file-&gt;open(QIODevice::WriteOnly))\r\n    {\r\n        QMessageBox::warning(this, tr(\"Warning\"),\r\n                             tr(\"file open error\"),\r\n                             QMessageBox::Yes);\r\n        qDebug() &lt;&lt; \"file open error\";\r\n        delete file;\r\n        file = 0;\r\n        return;\r\n   }\r\n   startRequest(url);\/\/\u8fdb\u884c\u8bf7\u6c42;\r\n   ui-&gt;progressBar-&gt;setValue(0);\r\n   ui-&gt;progressBar-&gt;show();\r\n}<\/pre>\n<h3>startRequest()\u7684\u5b9e\u73b0<\/h3>\n<p>\u901a\u8fc7\u53c2\u8003\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u5c31\u53ef\u4ee5\u53d1\u73b0\uff0c\u8be5\u51fd\u6570\u6ce8\u610f\u8fdb\u884c\u4e86\u4e24\u90e8\u5206\u7684\u5185\u5bb9\uff1a\u53d1\u9001\u4e0b\u8f7d\u8bf7\u6c42\u5e76\u83b7\u5f97\u6570\u636e\u56de\u590d\u548c\u51e0\u7ec4\u4fe1\u53f7\u548c\u69fd\u51fd\u6570\u4e4b\u95f4\u7684\u8fde\u63a5\u3002<\/p>\n<p>get()\u51fd\u6570\u5728\u4e0a\u6587\u4e2d\u5df2\u6709\u8bf4\u660e\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2aQNetworkReply\u7c7b\u7684\u5bf9\u8c61reply\u3002\u5f53\u6240\u6709\u7684\u6570\u636e\u4e0b\u8f7d\u5b8c\u6bd5\u540e\uff0cmanager\u5bf9\u8c61\u5c06\u53d1\u9001finished\u4fe1\u53f7\uff0c\u8fdb\u800c\u8c03\u7528httpFinished\u69fd\u51fd\u6570\u3002<\/p>\n<p>\u4e0a\u6587\u6240\u63cf\u8ff0\u7684Http\u5ba2\u6237\u7aef\u662f\u7b49\u5f85\u6240\u6709\u7684\u6570\u636e\u90fd\u4e0b\u8f7d\u5230\u5185\u5b58\u518d\u8bfb\u51fa\u5e76\u663e\u793a\uff0c\u800c\u672c\u6587\u6240\u63cf\u8ff0\u7684Http\u5ba2\u6237\u7aef\u5219\u5c06\u8bf7\u6c42\u7684\u6570\u636e\u8fdb\u884c\u5206\u6bb5\u4e0b\u8f7d\u5e76\u4fdd\u5b58\u3002\u56e0\u6b64\u6bcf\u5f53\u6709\u4e00\u90e8\u5206\u65b0\u6570\u636e\u5230\u8fbe\u672c\u5730\uff0creply\u5c31\u4f1a\u53d1\u9001readyRead\u4fe1\u53f7\uff0c\u8fdb\u800c\u8c03\u7528httpReadyRead\u69fd\u51fd\u6570\u5c06\u8fd9\u90e8\u5206\u65b0\u7684\u6570\u636e\u4fdd\u5b58\u5230\u672c\u5730\u3002\u4f7f\u7528\u8fd9\u79cd\u5206\u6bb5\u4e0b\u8f7d\u5e76\u4fdd\u5b58\u6570\u636e\u7684\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u7684\u8282\u7701\u5185\u5b58\u3002\u800c\u4e00\u65e6\u7f51\u7edc\u8bf7\u6c42\u6709\u6570\u636e\u8fd4\u56de\uff0creply\u5bf9\u8c61\u5c31\u4f1a\u53d1\u9001downloadProgress\u4fe1\u53f7\uff0c\u8fdb\u800c\u5f15\u53d1updataReadProcess\u69fd\u51fd\u6570\u5bf9\u8fdb\u5ea6\u663e\u793a\u6761\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<pre class=\"brush:c\">void widget::startRequest(QUrl url)\r\n{\r\n    reply = manager-&gt;get(QNetworkRequest(url));\r\n\r\n    connect(reply, SIGNAL(finished()),\r\n            this, SLOT(httpFinished()));\r\n\r\n    connect(reply, SIGNAL(readyRead()),\r\n            this, SLOT(httpReadyRead()));\r\n\r\n    connect(reply, SIGNAL(downloadProgress(qint64,qint64)),\r\n            this, SLOT(updataReadProcess(qint64,qint64)));\r\n}<\/pre>\n<h3>\u76f8\u5173\u69fd\u51fd\u6570<\/h3>\n<p>\u5f53\u6709\u4e00\u90e8\u5206\u65b0\u6570\u636e\u8fd4\u56de\u5230\u672c\u5730\u540e\uff0c\u5c31\u56de\u6267\u884chttpReadyRead\u51fd\u6570\u3002\u8be5\u51fd\u6570\u5c06\u8fd9\u90e8\u5206\u65b0\u5230\u8fbe\u5185\u5b58\u7684\u6570\u636e\u5199\u5165file\u5bf9\u8c61\u6240\u5bf9\u5e94\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<pre class=\"brush:c\">void widget::httpReadyRead()\r\n{\r\n    \/\/\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff1b\r\n    if (file)\r\n        file-&gt;write(reply-&gt;readAll());\r\n}<\/pre>\n<p>\u6bcf\u5f53\u8bf7\u6c42\u7684\u6570\u636e\u6709\u8fd4\u56de\u65f6\uff0c\u5c31\u6267\u884cupdataReadProcess\u51fd\u6570\u4ee5\u4fbf\u53ca\u65f6\u66f4\u65b0\u8fdb\u5ea6\u663e\u793a\u6761\u3002\u8be5\u51fd\u6570\u5305\u542b\u4e24\u4e2a\u53c2\u6570byteRead\u548ctotalBytes\u3002\u524d\u8005\u8868\u793a\u5f53\u524d\u63a5\u53d7\u5230\u7684\u6570\u636e\u603b\u91cf\uff0c\u540e\u8005\u8868\u793a\u5e94\u8be5\u4e0b\u8f7d\u7684\u6570\u636e\u603b\u91cf\u3002\u663e\u7136\uff0c\u968f\u7740byteRead\u7684\u589e\u52a0\uff0c\u8fdb\u5ea6\u663e\u793a\u6761\u4e5f\u4e0d\u65ad\u66f4\u65b0\u3002\u5f53byteRead\u548ctotalBytes\u76f8\u7b49\u65f6\uff0c\u8868\u793a\u4e0b\u8f7d\u5b8c\u6bd5\u3002<\/p>\n<pre class=\"brush:c\">void widget::updataReadProcess(qint64 byteRead, qint64 totalBytes)\r\n{\r\n    ui-&gt;progressBar-&gt;setMaximum(totalBytes);\/\/\u6700\u5927\u503c\uff1b\r\n    ui-&gt;progressBar-&gt;setValue(byteRead);\/\/\u5f53\u524d\u503c\r\n}<\/pre>\n<p>\u5f53\u6240\u6709\u6570\u636e\u90fd\u4e0b\u8f7d\u5b8c\u6bd5\u540e\uff0c\u5c31\u6267\u884chttpFinished\u51fd\u6570\u3002\u5728\u8be5\u51fd\u6570\u4e2d\u5c06\u9690\u85cf\u8fdb\u5ea6\u663e\u793a\u6761\uff0c\u5e76\u5c06\u7f13\u51b2\u533a\u7684\u6570\u636e\u6e05\u7a7a\u3002\u5e76\u4e14\u5173\u95ed\u6587\u4ef6\u5bf9\u8c61\uff0c\u518d\u91ca\u653e\u4e4b\u524d\u7533\u8bf7\u7684\u4e00\u4e9b\u6570\u636e\u7a7a\u95f4\u3002<\/p>\n<pre class=\"brush:c\">void widget::httpFinished()\r\n{\r\n    ui-&gt;progressBar-&gt;hide();\r\n    file-&gt;flush();\r\n    file-&gt;close();\r\n    reply-&gt;deleteLater();\r\n    reply = 0;\r\n    delete file;\r\n    file = 0;\r\n}<\/pre>\n<p>\u81f3\u6b64\uff0c\u57fa\u672c\u4e0a\u5b8c\u6210\u4e86\u4e00\u4e2aHttp\u4e0b\u8f7d\u5ba2\u6237\u7aef\u3002\u7406\u8bba\u4e0a\u53ef\u4ee5\u4e0b\u8f7d\u4efb\u4f55\u6570\u636e\uff0c\u4e0d\u8fc7\u6211\u5728\u6d4b\u8bd5\u4e2d\u53d1\u73b0\u6709\u4e9b\u5730\u5740\u4e0d\u80fd\u5982\u671f\u4e0b\u8f7d\uff08\u6bd4\u5982QQ\u7684\u4e0b\u8f7d\u94fe\u63a5\uff09\u3002<\/p>\n<p><strong>\u53c2\u8003\uff1a<\/strong><\/p>\n<p><em><strong>1. Qt Assistant<\/strong><\/em><\/p>\n<p><em><strong>2.\u00a0<a href=\"http:\/\/www.yafeilinux.com\/?p=734\">http:\/\/www.yafeilinux.com\/?p=734<\/a><\/strong><\/em><\/p>\n<p><em><strong>3. C++ GUI Qt 4\u7f16\u7a0b\uff08\u7b2c\u4e8c\u7248\uff09\uff1b \u7535\u5b50\u5de5\u4e1a\u51fa\u7248\u793e\uff1bBlanchette\uff0cJ\uff0cSummerfield\uff0cM \u8457\uff1b\u95eb\u950b\u6b23 \u7b49\u8bd1\uff1b<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fdb\u9636 \u4e0a\u6587\u7684Http\u5ba2\u6237\u7aef\u53ea\u80fd\u4e0b\u8f7d\u6307\u5b9a\u7f51\u5740\u7684\u6570\u636e\uff0c\u8fd9\u6837\u7684\u5ba2\u6237\u7aef\u5728\u4ea4\u4e92\u6027\u548c\u529f\u80fd\u6027\u4e0a\u90fd\u5f88\u5dee\u3002\u672c\u6587\u6240\u63cf\u8ff0\u7684\u7a0b\u5e8f\u5219\u5728\u8fd9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[199],"tags":[201,202,200],"class_list":["post-2637","post","type-post","status-publish","format-standard","hentry","category-gui","tag-c","tag-gui","tag-qt"],"views":6039,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=2637"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2637\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2637"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}