• <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, 評(píng)論 - 9, 引用 - 0
            數(shù)據(jù)加載中……

            qthread QNetworkAccessManager QEventLoop

            進(jìn)程通過QNetworkAccessManager 進(jìn)行http請(qǐng)求。代碼如下:

            //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)主進(jìn)程開辟線程來執(zhí)行http請(qǐng)求時(shí),卻無法成功。

            //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;
                }
            }

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

            2  通過connect設(shè)置信號(hào)槽,需要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();
            }

            這樣,就可以對(duì)http進(jìn)行請(qǐng)求了。

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

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

            狠狠色婷婷综合天天久久丁香 | 日本精品久久久久影院日本| 日韩精品久久久久久久电影蜜臀| 精品久久久久久国产三级| 国产精品青草久久久久福利99| a级毛片无码兔费真人久久| 精品国产乱码久久久久久郑州公司| 伊人久久大香线蕉av不卡| 香蕉久久av一区二区三区| 97精品久久天干天天天按摩| 精品无码久久久久久久动漫| 久久伊人精品青青草原高清| 一级A毛片免费观看久久精品| 久久超乳爆乳中文字幕| 国内精品久久久久久不卡影院| 国产精品狼人久久久久影院| 久久人人爽人人爽AV片| 久久精品国产亚洲av麻豆色欲| 久久久久亚洲精品天堂| 久久精品99久久香蕉国产色戒| 久久精品嫩草影院| 久久久久久A亚洲欧洲AV冫| www.久久精品| 久久精品国产只有精品66| 日产精品久久久久久久| 99精品久久精品| 精品久久综合1区2区3区激情| 久久夜色精品国产亚洲| 狠狠色婷婷久久一区二区三区| a级毛片无码兔费真人久久| 久久精品国产亚洲av麻豆蜜芽| 国产精品综合久久第一页| 亚洲午夜福利精品久久| 狠狠色丁香久久婷婷综合五月| 久久精品视屏| 精品人妻久久久久久888| 人妻少妇精品久久| 久久精品国产亚洲欧美| 99久久综合国产精品免费| 99久久精品免费看国产| 青青青国产成人久久111网站|