• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Cpper
            C/C++高級工程師 Android高級軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿
            QT附帶的例子比較好:
            class HttpWindow : public QDialog
            {
                Q_OBJECT
            public:
                HttpWindow(QWidget 
            *parent = 0);

                
            void startRequest(QUrl url);
            private slots:
                
            void downloadFile();
                
            void cancelDownload();
                
            void httpFinished();
                
            void httpReadyRead();
                
            void updateDataReadProgress(qint64 bytesRead, qint64 totalBytes);
                
            void enableDownloadButton();
                
            void slotAuthenticationRequired(QNetworkReply*,QAuthenticator *);
            private:
                QLabel 
            *statusLabel;
                QLabel 
            *urlLabel;
                QLineEdit 
            *urlLineEdit;
                QProgressDialog 
            *progressDialog;
                QPushButton 
            *downloadButton;
                QPushButton 
            *quitButton;
                QDialogButtonBox 
            *buttonBox;

                QUrl url;
                QNetworkAccessManager qnam;
                QNetworkReply 
            *reply;
                QFile 
            *file;
                
            int httpGetId;
                
            bool httpRequestAborted;
            };
            其中槽有:
            1.開始下載
            2.取消下載
            3.預備下載
            4.下載完成
            5.進度回調


            實現為:
            void HttpWindow::startRequest(QUrl url)
            {
                reply 
            = qnam.get(QNetworkRequest(url));
                connect(reply, SIGNAL(finished()),
                        
            this, SLOT(httpFinished()));
                connect(reply, SIGNAL(readyRead()),
                        
            this, SLOT(httpReadyRead()));
                connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
                        
            this, SLOT(updateDataReadProgress(qint64,qint64)));
            }
            該函數主要針對給定url綁定事件
            void HttpWindow::downloadFile()
            {
                url 
            = urlLineEdit->text();

                QFileInfo fileInfo(url.path());
                QString fileName 
            = fileInfo.fileName();
                fileName 
            = "downloadfile.dat";
                
            if(fileName.isEmpty())
                    fileName 
            = "index.html";

                
            if(QFile::exists(fileName)) {
                    
            if (QMessageBox::question(this, tr("HTTP"), 
                                              tr(
            "There already exists a file called %1 in "
                                                 
            "the current directory. Overwrite?").arg(fileName),
                                              QMessageBox::Yes
            |QMessageBox::No, QMessageBox::No)
                        
            == QMessageBox::No)
                        
            return;
                    QFile::remove(fileName);
                }

                file 
            = new QFile(fileName);
                
            if (!file->open(QIODevice::WriteOnly)) {
                    QMessageBox::information(
            this, tr("HTTP"),
                                             tr(
            "Unable to save the file %1: %2.")
                                             .arg(fileName).arg(file
            ->errorString()));
                    delete file;
                    file 
            = 0;
                    
            return;
                }

                progressDialog
            ->setWindowTitle(tr("HTTP"));
                progressDialog
            ->setLabelText(tr("Downloading %1.").arg(fileName));
                downloadButton
            ->setEnabled(false);

                
            // schedule the request
                httpRequestAborted = false;
                startRequest(url);
            }
            當點擊下載的時候,會執行該函數
            獲取url鏈接,生成本地文件,...
            void HttpWindow::cancelDownload()
            {
                statusLabel
            ->setText(tr("Download canceled."));
                httpRequestAborted 
            = true;
                reply
            ->abort();
                downloadButton
            ->setEnabled(true);
            }
            終止下載,主要函數是reply->abort();
            void HttpWindow::httpFinished()
            {
                
            if (httpRequestAborted) {
                    
            if (file) {
                        file
            ->close();
                        file
            ->remove();
                        delete file;
                        file 
            = 0;
                    }
                    reply
            ->deleteLater();
                    progressDialog
            ->hide();
                    
            return;
                }

                progressDialog
            ->hide();
                file
            ->flush();
                file
            ->close();


                QVariant redirectionTarget 
            = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
                
            if (reply->error()) {
                    file
            ->remove();
                    QMessageBox::information(
            this, tr("HTTP"),
                                             tr(
            "Download failed: %1.")
                                             .arg(reply
            ->errorString()));
                    downloadButton
            ->setEnabled(true);
                } 
            else if (!redirectionTarget.isNull()) {        
                    QUrl newUrl 
            = url.resolved(redirectionTarget.toUrl());
                    
            if (QMessageBox::question(this, tr("HTTP"),
                                              tr(
            "Redirect to %1 ?").arg(newUrl.toString()),
                                              QMessageBox::Yes 
            | QMessageBox::No) == QMessageBox::Yes) {
                        url 
            = newUrl;
                        reply
            ->deleteLater();
                        file
            ->open(QIODevice::WriteOnly);
                        file
            ->resize(0);
                        startRequest(url);
                        
            return;
                    }
                } 
            else {
                    QString fileName 
            = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
                    statusLabel
            ->setText(tr("Downloaded %1 to current directory.").arg(fileName));
                    downloadButton
            ->setEnabled(true);
                }

                reply
            ->deleteLater();
                reply 
            = 0;
                delete file;
                file 
            = 0;
            }
              下載結束動作
            void HttpWindow::httpReadyRead()
            {
                
            // this slot gets called every time the QNetworkReply has new data.
                
            // We read all of its new data and write it into the file.
                
            // That way we use less RAM than when reading it at the finished()
                
            // signal of the QNetworkReply
                if (file)
                    file
            ->write(reply->readAll());
            }
            寫文件回調
            void HttpWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
            {
                
            if (httpRequestAborted)
                    
            return;

                progressDialog
            ->setMaximum(totalBytes);
                progressDialog
            ->setValue(bytesRead);
            }
            進度回調

            要點:
            1.針對QNetReply綁定需要的信號和槽
            2.實現需要的槽函數

            posted on 2011-11-16 20:23 ccsdu2009 閱讀(13013) 評論(2)  編輯 收藏 引用 所屬分類: QT編程
            Comments
            • # re: QT學習筆記-29.使用QT HTTP下載網絡文件
              22
              Posted @ 2015-09-29 10:56
              這段代碼怎么設置文件下載保存的路經呢?  回復  更多評論   
            • # re: QT學習筆記-29.使用QT HTTP下載網絡文件
              ccsdu2009
              Posted @ 2015-10-08 08:37
              @22
              downloadFile()函數  回復  更多評論   
             
            久久久亚洲精品蜜桃臀| 久久久国产亚洲精品| 国内精品综合久久久40p| 国产精品内射久久久久欢欢 | 少妇久久久久久久久久| 久久国产香蕉一区精品| 91精品国产91久久| 国产精品久久亚洲不卡动漫| 午夜精品久久久久久中宇| 偷偷做久久久久网站| 中文字幕久久亚洲一区| 亚洲第一永久AV网站久久精品男人的天堂AV| 国内精品久久久久久久久电影网| 久久国产精品99精品国产| 三级韩国一区久久二区综合| 久久婷婷五月综合成人D啪| 久久亚洲2019中文字幕| 深夜久久AAAAA级毛片免费看| 久久久久人妻一区精品| 久久久无码精品午夜| 三级韩国一区久久二区综合 | 久久丫忘忧草产品| 亚洲级αV无码毛片久久精品| 少妇高潮惨叫久久久久久| 欧美亚洲色综久久精品国产| 亚洲va国产va天堂va久久| 99久久99久久精品免费看蜜桃 | 久久99国产精品99久久| 亚洲国产精品久久久久网站| 久久九色综合九色99伊人| 欧美久久亚洲精品| 久久久久99精品成人片牛牛影视| 久久99精品久久久久久9蜜桃| 亚洲精品综合久久| 久久久噜噜噜久久中文字幕色伊伊 | 久久精品国产99久久无毒不卡| 亚洲精品美女久久777777| 国产亚洲精久久久久久无码| 色偷偷偷久久伊人大杳蕉| 99久久99久久久精品齐齐| 999久久久免费国产精品播放|