• <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
            數據加載中……

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

            }



            但當主進程開辟線程來執行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;
                }
            }

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

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

            最終的修改代碼如下:
            //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進行請求了。

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

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

            久久国产AVJUST麻豆| 久久免费精品视频| 久久午夜综合久久| 久久久亚洲裙底偷窥综合| 中文精品99久久国产| 国产精品99精品久久免费| 国产精品女同一区二区久久| 久久久精品久久久久影院| 久久久亚洲欧洲日产国码二区| 色综合久久久久网| 午夜人妻久久久久久久久| 94久久国产乱子伦精品免费| 久久久亚洲裙底偷窥综合| 国产精品一区二区久久精品无码| 国内精品久久久久影院老司| 久久九九亚洲精品| 亚洲va久久久噜噜噜久久天堂| 久久99精品国产麻豆蜜芽| 久久综合香蕉国产蜜臀AV| 久久国产精品波多野结衣AV| 高清免费久久午夜精品| 精品多毛少妇人妻AV免费久久| 久久精品99无色码中文字幕| 国产亚洲美女精品久久久久狼| 伊人久久大香线蕉成人| 久久精品国产一区二区电影| 99久久精品国产一区二区三区| 无码精品久久一区二区三区| 久久久久香蕉视频| 狠狠色丁香婷婷久久综合五月| 99久久亚洲综合精品成人| 国内精品久久九九国产精品| 久久99精品久久久久子伦| 一本色道久久99一综合| 99久久精品影院老鸭窝| 国产精品一区二区久久不卡| 久久精品国产日本波多野结衣| 女人高潮久久久叫人喷水| 老司机午夜网站国内精品久久久久久久久| 91麻精品国产91久久久久| 久久精品国产亚洲精品|