• <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 閱讀(3678) 評論(0)  編輯 收藏 引用 所屬分類: Qt

            久久久无码人妻精品无码| 曰曰摸天天摸人人看久久久| 久久免费观看视频| 一本久久综合亚洲鲁鲁五月天| 香港aa三级久久三级老师2021国产三级精品三级在 | 亚洲а∨天堂久久精品| 一本久道久久综合狠狠躁AV| 无码人妻久久久一区二区三区| 久久国产高清字幕中文| 久久综合日本熟妇| 国产精品久久久久aaaa| 久久青青草视频| 久久精品嫩草影院| 久久精品国产精品亚洲精品| 久久久久久久综合日本亚洲| 久久综合色区| 精品久久久久久国产牛牛app| 99久久香蕉国产线看观香| 久久精品视频免费| 精品久久久久久成人AV| 亚洲精品乱码久久久久久不卡| 国产产无码乱码精品久久鸭| 亚州日韩精品专区久久久| 7777久久亚洲中文字幕| 国内精品人妻无码久久久影院导航| 欧美亚洲国产精品久久蜜芽 | 久久精品国产精品青草app| 亚洲国产精品嫩草影院久久| 久久精品成人免费看| 久久精品国产亚洲av水果派 | 国产91色综合久久免费| 超级碰碰碰碰97久久久久| 精品久久久久久99人妻| 亚洲国产成人久久精品影视| 久久精品人人做人人爽电影蜜月 | 久久午夜福利电影| 狠狠精品干练久久久无码中文字幕 | 色综合久久无码中文字幕| 99久久国产宗和精品1上映| 中文字幕无码久久精品青草| 久久狠狠一本精品综合网|