• <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>
            隨筆 - 70, 文章 - 0, 評論 - 9, 引用 - 0
            數(shù)據(jù)加載中……

            qthread QNetworkAccessManager QEventLoop

            進程通過QNetworkAccessManager 進行http請求。代碼如下:

            //qhttp.h
            #ifndef QHTTP_H
            #define QHTTP_H

            #include 
            <QtGui/QWidget>
            #include 
            "ui_qhttp.h"
            #include 
            <QNetworkAccessManager>
            #include 
            <QUrl>

            class QPushButton;
            class QDialogButtonBox;
            class QNetworkReply;

            class qhttp : public QWidget
            {
                Q_OBJECT

            public:
                qhttp(QWidget 
            *parent = 0);
                
            ~qhttp();

                
            void startRequest(QUrl url);

            private:
                Ui::qhttpClass ui;

                QPushButton 
            *downloadButton;
                QDialogButtonBox 
            *buttonBox;

                QUrl url;
                QNetworkAccessManager qnam;
                QNetworkReply 
            *reply;

            private slots:
                
            void downloadFile();
                
            void httpFinished();
            }
            ;

            #endif // QHTTP_H


            //qhppt.cpp
            #include "qhttp.h"
            #include 
            <QtGui>
            #include 
            <QtNetwork>
            #include 
            <iostream>

            using namespace std;

            qhttp::qhttp(QWidget 
            *parent)
                : QWidget(parent)
            {
                ui.setupUi(
            this);

                downloadButton 
            = new QPushButton(tr("Download"));
                downloadButton
            ->setDefault(true);


                buttonBox 
            = new QDialogButtonBox;
                buttonBox
            ->addButton(downloadButton, QDialogButtonBox::ActionRole);

                connect(downloadButton, SIGNAL(clicked()), 
            this, SLOT(downloadFile()));

                QVBoxLayout 
            *mainLayout = new QVBoxLayout;
                mainLayout
            ->addWidget(buttonBox);
                setLayout(mainLayout);
            }


            qhttp::
            ~qhttp()
            {

            }


            void qhttp::downloadFile()
            {
                cout
            <<"downloadFile"<<endl;
                url 
            = "http://www.baidu.com/";
                startRequest(url);
            }


            void qhttp::startRequest(QUrl url)
            {
                cout<<"startRequest"<<endl;
                reply = qnam.get(QNetworkRequest(url));
                connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
            }

            void qhttp::httpFinished()
            {
                cout
            <<"httpFinished"<<endl;
                
            if(reply->error())
                
            {
                    cout
            <<"error:"<<qPrintable(reply->errorString())<<endl;
                }

                
            else
                
            {
                    cout
            <<"right"<<endl;
                }

            }



            但當(dāng)主進程開辟線程來執(zhí)行http請求時,卻無法成功。

            //qhttp.h
            #ifndef QHTTP_H
            #define QHTTP_H

            #include <QtGui/QWidget>
            #include "ui_qhttp.h"
            #include <QNetworkAccessManager>
            #include <QUrl>

            class QPushButton;
            class QDialogButtonBox;
            class QNetworkReply;
            class qhttpThread;

            class qhttp : public QWidget
            {
                Q_OBJECT

            public:
                qhttp(QWidget *parent = 0);
                ~qhttp();

                void startRequest(QUrl url);

            private:
                Ui::qhttpClass ui;

                QPushButton *downloadButton;
                QDialogButtonBox *buttonBox;

                QUrl url;
                QNetworkAccessManager qnam;
                QNetworkReply *reply;
                qhttpThread *httpthread;

            private slots:
                void downloadFile();
                void httpFinished();
            };

            #endif // QHTTP_H



            //qhttp.cpp
            #include "qhttp.h"
            #include "qhttpThread.h"
            #include <QtGui>
            #include <QtNetwork>
            #include <iostream>

            using namespace std;

            qhttp::qhttp(QWidget *parent)
                : QWidget(parent)
            {
                ui.setupUi(this);

                downloadButton = new QPushButton(tr("Download"));
                downloadButton->setDefault(true);


                buttonBox = new QDialogButtonBox;
                buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);

                connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));

                QVBoxLayout *mainLayout = new QVBoxLayout;
                mainLayout->addWidget(buttonBox);
                setLayout(mainLayout);
            }

            qhttp::~qhttp()
            {

            }

            void qhttp::downloadFile()
            {
                cout<<"downloadFile"<<endl;
                //url = "http://www.baidu.com/";
                //startRequest(url);


                httpthread = new qhttpThread(this);
                httpthread->start();

            }

            void qhttp::startRequest(QUrl url)
            {
                cout<<"startRequest"<<endl;
                reply = qnam.get(QNetworkRequest(url));
                connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
            }

            void qhttp::httpFinished()
            {
                cout<<"httpFinished"<<endl;
                if(reply->error())
                {
                    cout<<"error:"<<qPrintable(reply->errorString())<<endl;
                }
                else
                {
                    cout<<"right"<<endl;
                }
            }

            此時會出現(xiàn)程序運行崩潰、沒有執(zhí)行http請求任務(wù)等情況。
            簡單說一下注意事項:
            1  線程中的run函數(shù)返回后線程即結(jié)束,根本無法等到reply的finished信號。因此需要加入QEventLoop來掛起線程。

            2  通過connect設(shè)置信號槽,需要QObject的支持,因此在構(gòu)造函數(shù)里與頭文件中需要修改代碼。

            最終的修改代碼如下:
            //qhttpThread.h
            #ifndef QHTTPTHREAD_H_
            #define QHTTPTHREAD_H_

            #include <QThread>
            #include <QObject>
            #include <QNetworkAccessManager>
            #include <QUrl>

            class QNetworkReply;
            class QEventLoop;

            class qhttpThread : public QThread {
                Q_OBJECT
            public:
                qhttpThread(QObject *parent = 0);
                virtual ~qhttpThread();

                void run();

            private:
                QUrl url;
                QNetworkAccessManager qnam;
                QNetworkReply *reply;
                QEventLoop *loop;

                void startRequest(QUrl url);

            private slots:
                void httpFinished();
            };

            #endif /* QHTTPTHREAD_H_ */




            //qhttpThread.cpp
            #include "qhttpThread.h"
            #include <QtNetwork>
            #include <QEventLoop>
            #include <iostream>
            using namespace std;

            qhttpThread::qhttpThread(QObject *parent) : QThread(parent) {
                // TODO Auto-generated constructor stub

            }

            qhttpThread::~qhttpThread() {
                // TODO Auto-generated destructor stub
            }

            void qhttpThread::run()
            {
                cout<<"run"<<endl;
                url = "http://www.baidu.com/";
                startRequest(url);


                exec();
                cout<<"run end"<<endl;
            }

            void qhttpThread::startRequest(QUrl url)
            {
                cout<<"startRequest"<<endl;

                loop = new QEventLoop;
                reply = qnam.get(QNetworkRequest(url));
                connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));

                loop->exec();
                cout<<"startRequest end"<<endl;
            }

            void qhttpThread::httpFinished()
            {
                cout<<"httpFinished"<<endl;
                if(reply->error())
                {
                    cout<<"error:"<<qPrintable(reply->errorString())<<endl;
                }
                else
                {
                    cout<<"right"<<endl;
                }
                loop->quit();
            }

            這樣,就可以對http進行請求了。

            但在真正的執(zhí)行中,當(dāng)關(guān)閉界面程序時出現(xiàn)彈出框,提示:This application has requested the Runtime to terninate it in an unusual way.
            原因還在查找中。

            posted on 2011-01-04 09:28 seahouse 閱讀(3675) 評論(0)  編輯 收藏 引用 所屬分類: Qt

            国色天香久久久久久久小说| 成人午夜精品无码区久久| 亚洲一区中文字幕久久| 精品水蜜桃久久久久久久| 日韩电影久久久被窝网| 麻豆亚洲AV永久无码精品久久| 久久精品国产免费| 久久久久人妻精品一区三寸蜜桃| 无码人妻少妇久久中文字幕蜜桃 | 精品久久久久久国产潘金莲 | 囯产精品久久久久久久久蜜桃 | 久久精品国产第一区二区| 精品多毛少妇人妻AV免费久久| 久久99国产乱子伦精品免费| 色婷婷综合久久久久中文字幕 | 久久99国产精品99久久 | 国产午夜精品久久久久九九电影| 一级a性色生活片久久无少妇一级婬片免费放 | 狠狠人妻久久久久久综合蜜桃| 亚洲AV日韩AV永久无码久久| 久久久久久国产精品免费免费| 久久精品国产精品国产精品污| 久久天天躁狠狠躁夜夜躁2O2O| 国产精品久久久久免费a∨| 久久AAAA片一区二区| www.久久精品| 无码国内精品久久人妻| 亚洲日本久久久午夜精品| 久久久噜噜噜久久中文字幕色伊伊 | 亚洲精品乱码久久久久久蜜桃| 青青草国产精品久久久久| 国产亚洲精久久久久久无码| 亚洲AV无码久久精品色欲| 一本久久a久久精品vr综合| 久久精品国产精品亚洲精品| 性高朝久久久久久久久久| 久久久久亚洲AV综合波多野结衣| 久久久久久亚洲精品不卡 | 人妻少妇精品久久| 久久亚洲国产最新网站| 久久笫一福利免费导航 |