{"id":2680,"date":"2011-03-02T09:29:18","date_gmt":"2011-03-02T01:29:18","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2680"},"modified":"2011-03-03T11:12:22","modified_gmt":"2011-03-03T03:12:22","slug":"%e5%9f%ba%e4%ba%8eqt%e7%9a%84ftp%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2680","title":{"rendered":"\u57fa\u4e8eQt\u7684FTP\u7f16\u7a0b"},"content":{"rendered":"<p>\u5728Qt\u4e2d\uff0c\u4e0e\u6587\u4ef6\u4f20\u8f93\u534f\u8bae\uff08FTP\uff09\u76f8\u5bf9\u5e94\u7684\u7c7b\u4e3aQFtp\u3002\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0e\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d\u6709\u5173\u7684\u65b9\u6cd5\uff0c\u8fd9\u4f7f\u5f97\u6587\u4ef6\u7684\u4e0a\u4f20\u4e0e\u4e0b\u8f7d\u53d8\u5f97\u5341\u5206\u65b9\u4fbf\u3002\u672c\u6587\u6240\u63cf\u8ff0\u7684FTP\u5ba2\u6237\u7aef\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u662f\uff1a\u9996\u5148\u8f93\u5165\u6b63\u786e\u7684FTP\u670d\u52a1\u5668\u5730\u5740\uff0c\u7528\u6237\u540d\u4ee5\u53ca\u5bc6\u7801\uff1b\u767b\u5f55\u6210\u529f\u540e\u5c06\u663e\u793a\u670d\u52a1\u5668\u4e2d\u7684\u6587\u4ef6\u5217\u8868\uff1b\u70b9\u51fb\u4e0b\u8f7d\u6309\u94ae\u5bf9\u67d0\u4e2a\u6587\u4ef6\u8fdb\u884c\u4e0b\u8f7d\u3002\u7528\u6237\u754c\u9762\u53ef\u53c2\u8003\u4e0b\u56fe\uff1a<\/p>\n<p><a rel=\"attachment wp-att-2692\" href=\"http:\/\/edsionte.com\/techblog\/archives\/2680\/screenshot-widget-1\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2692\" title=\"Screenshot-Widget-1\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/03\/Screenshot-Widget-1-300x236.png\" alt=\"\" width=\"300\" height=\"236\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/03\/Screenshot-Widget-1-300x236.png 300w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/03\/Screenshot-Widget-1.png 514w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u7528\u6237\u754c\u9762\u8bbe\u8ba1\u53ef\u89c1\u672c\u6587\u53c2\u80031\u548c\u53c2\u80032\uff0c\u63a5\u4e0b\u6765\u5c06\u5bf9\u5177\u4f53\u7684\u5b9e\u73b0\u8fc7\u7a0b\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<h3>\u6784\u9020\u51fd\u6570<\/h3>\n<p>\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u5bf9\u7528\u6237\u754c\u9762\u8fdb\u884c\u4e86\u521d\u59cb\u5316\uff0c\u5e76\u9690\u85cf\u4e86\u8fdb\u5ea6\u663e\u793a\u6761\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u5c06itemActivated\u4fe1\u53f7\u548cprocessItem\u69fd\u51fd\u6570\u8fdb\u884c\u4e86\u8fde\u63a5\u3002\u5f53\u7528\u6237\u53cc\u51fb\uff08\u6216\u5355\u51fb\uff0c\u4f9d\u636e\u5177\u4f53OS\uff09TreeWidget\u4e2d\u7684\u67d0\u4e00\u6761\u4fe1\u606f\u65f6\uff0cQTreeWidget\u7c7b\u7684\u5bf9\u8c61fileList\u5c31\u4f1a\u53d1\u9001itemActivated\u4fe1\u53f7\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    ui-&gt;progressBar-&gt;setValue(0);\r\n\r\n    connect(ui-&gt;fileList, SIGNAL(itemActivated(QTreeWidgetItem*,int)),\r\n            this, SLOT(processItem(QTreeWidgetItem*,int)));\r\n}<\/pre>\n<h3>on_connectButton_clicked()\u7684\u5b9e\u73b0<\/h3>\n<p>\u5728\u8fde\u63a5\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u7684\u69fd\u51fd\u6570\u4e2d\u4e3b\u8981\u5b8c\u6210\u4e86\u4e24\u90e8\u5206\u5de5\u4f5c\uff1a\u767b\u5f55FTP\u670d\u52a1\u5668\u4ee5\u53ca\u51e0\u4e2a\u4fe1\u53f7\u548c\u69fd\u51fd\u6570\u4e4b\u95f4\u7684\u8fde\u63a5\uff0c\u8fd9\u51e0\u4e2a\u4fe1\u53f7\u5747\u7531FTP\u5bf9\u8c61\u53d1\u51fa\u3002\u5f53FTP\u547d\u4ee4\u5f00\u59cb\u6267\u884c\u65f6\uff0c\u4fbf\u53d1\u51facommandStarted\u4fe1\u53f7\uff1b\u5f53FTP\u7684\u547d\u4ee4\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u4fbf\u53d1\u51facommandFinished\u4fe1\u53f7\uff1b\u5f53\u4f7f\u7528list\u547d\u4ee4\u5217\u8868\u663e\u793a\u76ee\u5f55\u7684\u5185\u5bb9\u65f6\u5019\uff0clistInfo\u4fe1\u53f7\u5c06\u53d1\u51fa\uff1b\u6700\u540e\u5728\u6570\u636e\u4f20\u8f93\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u4e0d\u65ad\u7684\u53d1\u51fadataTransferProgress\u4fe1\u53f7\u4ee5\u4fbf\u66f4\u65b0\u8fdb\u5ea6\u663e\u793a\u6761\u3002<\/p>\n<p>\u7531\u4e8eQFtp\u7c7b\u4e2d\u5c01\u88c5\u4e86\u8bb8\u591a\u4e0eFTP\u6709\u5173\u6210\u5458\u51fd\u6570\uff0c\u56e0\u6b64\u4f7f\u7528connectToHost\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u767b\u5f55\u5230FTP\u670d\u52a1\u5668\uff0c\u4f7f\u7528login\u51fd\u6570\u53ef\u4ee5\u767b\u5f55\u5230\u670d\u52a1\u5668\u3002<\/p>\n<pre class=\"brush:c\">void Widget::on_connectButton_clicked()\r\n{\r\n    ui-&gt;fileList-&gt;clear();\r\n    currentPath.clear();\r\n    isDirectory.clear();\r\n    ftp = new QFtp(this);\r\n\r\n    connect(ftp, SIGNAL(commandFinished(int,bool)),\r\n            this, SLOT(ftpCommandFinished(int,bool)));\r\n    connect(ftp, SIGNAL(listInfo(QUrlInfo)), this, SLOT(addToList(QUrlInfo)));\r\n    connect(ftp, SIGNAL(dataTransferProgress(qint64,qint64)),\r\n            this, SLOT(updateDataTransferProgress(qint64,qint64)));\r\n\r\n    QString ftpServer = ui-&gt;ftpServerLineEdit-&gt;text();\r\n    QString userName = ui-&gt;userNameLineEdit-&gt;text();\r\n    QString passWord = ui-&gt;passWordLineEdit-&gt;text();\r\n\r\n    ftp-&gt;connectToHost(ftpServer, 21);\/\/\u8fde\u63a5\u5230\u670d\u52a1\u5668\uff0c\u9ed8\u8ba4\u7aef\u53e3\u662f21\uff1b\r\n    ftp-&gt;login(userName, passWord);\/\/\u767b\u5f55;\r\n}<\/pre>\n<h3>ftpCommandFinished()\u7684\u5b9e\u73b0<\/h3>\n<p>\u5f53FTP\u547d\u4ee4\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5c06\u6267\u884cftpCommandFinished\u51fd\u6570\u3002\u4e0e\u672c\u6587\u6240\u63cf\u8ff0\u7684\u547d\u4ee4\u6709ConnectToHost()\u3001Login()\u3001Get()\u3001Close()\u548cList()\u3002\u901a\u8fc7currentCommand\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u5177\u4f53\u5f97\u5230\u5f53\u524d\u6267\u884c\u5b8c\u6bd5\u547d\u4ee4\u7684id\uff0c\u56e0\u6b64\u6839\u636e\u4e0d\u540c\u547d\u4ee4\u7684id\u5728\u7528\u6237\u754c\u9762\u4e0a\u663e\u793a\u4e0d\u540c\u7684\u547d\u4ee4\u72b6\u6001\u3002<\/p>\n<pre class=\"brush:c\">void Widget::ftpCommandFinished(int, bool error)\r\n{\r\n    if (ftp-&gt;currentCommand() == QFtp::ConnectToHost) {\r\n        if (error)\r\n            ui-&gt;label-&gt;setText(tr(\"connecting error %1\").arg(ftp-&gt;errorString()));\r\n        else\r\n            ui-&gt;label-&gt;setText(tr(\"connecting success!\"));\r\n    }\r\n\r\n    if (ftp-&gt;currentCommand() == QFtp::Login) {\r\n        if (error)\r\n            ui-&gt;label-&gt;setText(tr(\"login error\").arg(ftp-&gt;errorString()));\r\n        else {\r\n            ui-&gt;label-&gt;setText(tr(\"login success!\"));\r\n            ftp-&gt;list();\r\n        }\r\n    }\r\n    \/\/\u90e8\u5206\u4ee3\u7801\u7701\u7565\r\n}<\/pre>\n<h3>on_downloadButton_clicked()\u7684\u5b9e\u73b0<\/h3>\n<p>\u5f53\u5355\u51fb\u4e0b\u8f7d\u6309\u94ae\u540e\uff0c\u5c06\u8fdb\u5165\u8be5\u51fd\u6570\u8fdb\u884c\u76f8\u5e94\u6587\u4ef6\u7684\u4e0b\u8f7d\u3002\u9996\u5148\u901a\u8fc7fileList\u5bf9\u8c61\u4e2d\u7684\u6210\u5458\u51fd\u6570\u5f97\u5230\u8981\u4e0b\u8f7d\u6587\u4ef6\u7684\u540d\u79f0\uff0c\u5728\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u6700\u540e\u901a\u8fc7get\u51fd\u6570\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<pre class=\"brush:c\">void Widget::on_downloadButton_clicked()\r\n{\r\n    QString fileName = ui-&gt;fileList-&gt;currentItem()-&gt;text(0);\r\n    file = new QFile(fileName);\r\n    if (!file-&gt;open(QIODevice::WriteOnly)) {\r\n        delete file;\r\n        return;\r\n    }\r\n\r\n    ui-&gt;downloadButton-&gt;setEnabled(false);\r\n    ftp-&gt;get(ui-&gt;fileList-&gt;currentItem()-&gt;text(0), file);\r\n}<\/pre>\n<h3>addToList()\u7684\u5b9e\u73b0<\/h3>\n<p>\u5f53\u5bf9FTP\u670d\u52a1\u5668\u4e0a\u67d0\u4e2a\u76ee\u5f55\u6267\u884clist()\u547d\u4ee4\u65f6\uff0c\u5bf9\u4e8elist()\u627e\u5230\u7684\u6bcf\u4e00\u4e2a\u6587\u4ef6\u90fd\u4f1a\u53d1\u51falistInfo()\u4fe1\u53f7\u3002\u8be5\u4fe1\u53f7\u4f1a\u4f7f\u5f97addToList\u51fd\u6570\u6267\u884c\uff0c\u4ee5\u4fbf\u5728treeWidget\u4e0a\u589e\u52a0\u4e00\u6761\u65b0\u7684\u6587\u4ef6\u4fe1\u606f\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u8be5\u51fd\u6570\u9996\u5148\u6839\u636eurlInfo\u53c2\u6570\u5efa\u7acb\u4e00\u6761\u65b0\u7684\u6587\u4ef6\u4fe1\u606f\uff0c\u7136\u540e\u518d\u5229\u7528addTopLevelItem\u6210\u5458\u51fd\u6570\u5c06\u8be5\u6761\u65b0\u7684\u6587\u4ef6\u4fe1\u606f\u52a0\u5165\u5230treeWidget\u6700\u9876\u7aef\u3002<\/p>\n<pre class=\"brush:c\">void Widget::addToList(const QUrlInfo &amp;urlInfo)\r\n{\r\n    QTreeWidgetItem *item = new QTreeWidgetItem;\r\n    item-&gt;setText(0, urlInfo.name());\r\n    item-&gt;setText(1, QString::number(urlInfo.size()));\r\n    item-&gt;setText(2, urlInfo.owner());\r\n    item-&gt;setText(4, urlInfo.lastModified().toString(\"mmm dd yyyy\"));\r\n    QPixmap pixmap(urlInfo.isDir() ? \"..\/dir.png\" : \"..\/file.png\");\r\n    item-&gt;setIcon(0, pixmap);\r\n    isDirectory[urlInfo.name()] = urlInfo.isDir();\r\n\r\n    \/\/\u5b58\u50a8\u8be5\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55\u7684\u4fe1\u606f\uff1b\r\n    ui-&gt;fileList-&gt;addTopLevelItem(item);\r\n    if (!ui-&gt;fileList-&gt;currentItem()) {\r\n        ui-&gt;fileList-&gt;setCurrentItem(ui-&gt;fileList-&gt;topLevelItem(0));\r\n        ui-&gt;fileList-&gt;setEnabled(true);\r\n    }\r\n}<\/pre>\n<h3>processItem()\u7684\u5b9e\u73b0<\/h3>\n<p>\u5f53\u53cc\u51fb\u6587\u4ef6\u5217\u8868\u4e2d\u67d0\u4e00\u9879\u65f6\u5019\uff0c\u5982\u679c\u8be5\u9879\u6240\u663e\u793a\u7684\u6587\u4ef6\u4e3a\u76ee\u5f55\uff0c\u5219\u8fdb\u5165\u8be5\u76ee\u5f55\u4e2d\u663e\u793a\u5176\u5185\u90e8\u7684\u6587\u4ef6\u5217\u8868\u3002\u4e0a\u8ff0\u7684\u5185\u5bb9\u5373\u4e3aprocessItem\u51fd\u6570\u6240\u8981\u5b8c\u6210\u7684\u5de5\u4f5c\u3002<\/p>\n<pre class=\"brush:c\">void Widget::processItem(QTreeWidgetItem *item, int)\r\n{\r\n    \/\/\u6253\u5f00\u4e00\u4e2a\u76ee\u5f55;\r\n    QString name = item-&gt;text(0);\r\n    if (isDirectory.value(name)) {\r\n        ui-&gt;fileList-&gt;clear();\r\n        isDirectory.clear();\r\n        currentPath += '\/';\r\n        currentPath += name;\r\n        ftp-&gt;cd(name);\r\n        ftp-&gt;list();\r\n        ui-&gt;cdToParentButton-&gt;setEnabled(true);\r\n    }\r\n}<\/pre>\n<h3>updateDataTransferProgress()\u7684\u5b9e\u73b0<\/h3>\n<p>\u6bcf\u5f53FTP\u670d\u52a1\u5668\u7aef\u7684\u6570\u636e\u8fd4\u56de\u65f6\uff0c\u5c31\u5f15\u53d1\u8be5\u51fd\u6570\u7684\u6267\u884c\u3002\u8be5\u51fd\u6570\u7684\u4e3b\u8981\u5de5\u4f5c\u5373\u66f4\u65b0\u8fdb\u5ea6\u6761\u7684\u663e\u793a\u3002<\/p>\n<pre class=\"brush:c\">void Widget::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes)\r\n{\r\n    ui-&gt;progressBar-&gt;setMaximum(totalBytes);\r\n    ui-&gt;progressBar-&gt;setValue(readBytes);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u672a\u5b8c\u5f85\u7eed\uff5e<br \/>\n<strong>\u53c2\u8003\uff1a<\/strong><\/p>\n<p><em><strong>1. Qt Assistant<\/strong><\/em><\/p>\n<p><em><strong>2. <a href=\"http:\/\/www.yafeilinux.com\/?p=757\">http:\/\/www.yafeilinux.com\/?p=757<\/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>\u5728Qt\u4e2d\uff0c\u4e0e\u6587\u4ef6\u4f20\u8f93\u534f\u8bae\uff08FTP\uff09\u76f8\u5bf9\u5e94\u7684\u7c7b\u4e3aQFtp\u3002\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0e\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d\u6709\u5173\u7684\u65b9\u6cd5\uff0c\u8fd9\u4f7f [&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,204,202,200],"class_list":["post-2680","post","type-post","status-publish","format-standard","hentry","category-gui","tag-c","tag-ftp","tag-gui","tag-qt"],"views":12006,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2680","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=2680"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2680\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2680"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}