• <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++高級(jí)工程師 Android高級(jí)軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語(yǔ)言 程序猿
            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.開(kāi)始下載
            2.取消下載
            3.預(yù)備下載
            4.下載完成
            5.進(jìn)度回調(diào)


            實(shí)現(xiàn)為:
            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)));
            }
            該函數(shù)主要針對(duì)給定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);
            }
            當(dāng)點(diǎn)擊下載的時(shí)候,會(huì)執(zhí)行該函數(shù)
            獲取url鏈接,生成本地文件,...
            void HttpWindow::cancelDownload()
            {
                statusLabel
            ->setText(tr("Download canceled."));
                httpRequestAborted 
            = true;
                reply
            ->abort();
                downloadButton
            ->setEnabled(true);
            }
            終止下載,主要函數(shù)是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;
            }
              下載結(jié)束動(dòng)作
            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());
            }
            寫(xiě)文件回調(diào)
            void HttpWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
            {
                
            if (httpRequestAborted)
                    
            return;

                progressDialog
            ->setMaximum(totalBytes);
                progressDialog
            ->setValue(bytesRead);
            }
            進(jìn)度回調(diào)

            要點(diǎn):
            1.針對(duì)QNetReply綁定需要的信號(hào)和槽
            2.實(shí)現(xiàn)需要的槽函數(shù)

            posted on 2011-11-16 20:23 ccsdu2009 閱讀(13012) 評(píng)論(2)  編輯 收藏 引用 所屬分類(lèi): QT編程
            Comments
            • # re: QT學(xué)習(xí)筆記-29.使用QT HTTP下載網(wǎng)絡(luò)文件
              22
              Posted @ 2015-09-29 10:56
              這段代碼怎么設(shè)置文件下載保存的路經(jīng)呢?  回復(fù)  更多評(píng)論   
            • # re: QT學(xué)習(xí)筆記-29.使用QT HTTP下載網(wǎng)絡(luò)文件
              ccsdu2009
              Posted @ 2015-10-08 08:37
              @22
              downloadFile()函數(shù)  回復(fù)  更多評(píng)論   
             
            久久青青草原亚洲av无码app| 精品无码久久久久久尤物| 69久久精品无码一区二区| 久久久久亚洲AV无码观看 | 丁香五月综合久久激情| 无码久久精品国产亚洲Av影片| 思思久久99热免费精品6| 久久成人国产精品一区二区| 9191精品国产免费久久| 94久久国产乱子伦精品免费| 亚洲国产精品一区二区久久| 久久最近最新中文字幕大全 | 中文字幕成人精品久久不卡| 激情伊人五月天久久综合| 国产人久久人人人人爽| 亚洲成色WWW久久网站| 人人狠狠综合久久88成人| 久久夜色精品国产噜噜亚洲a| 91久久精品国产免费直播| 久久综合九色综合精品| 久久亚洲国产午夜精品理论片 | 久久精品嫩草影院| 99久久精品国产一区二区| 国产呻吟久久久久久久92| 久久久综合香蕉尹人综合网| 亚洲中文字幕伊人久久无码| 久久久久av无码免费网| 久久久久亚洲av无码专区| 久久精品国产99国产精品澳门| 88久久精品无码一区二区毛片| 久久久中文字幕日本| 亚洲国产精品无码久久一区二区| 国产精品无码久久久久久| 国产福利电影一区二区三区久久久久成人精品综合| 久久免费高清视频| 亚洲伊人久久综合影院| 久久精品国产亚洲av麻豆小说| 国产精品成人精品久久久| 中文字幕日本人妻久久久免费 | 国产精品久久久久乳精品爆| 午夜视频久久久久一区|