forked from GLDsuh-a/qt-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
litz-a
committed
May 27, 2017
1 parent
f04ec80
commit 6f551ee
Showing
7 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,23 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2015-08-26T23:27:46 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT = core gui | ||
QT = network | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT = widgets | ||
|
||
TARGET = HttpDemo | ||
TEMPLATE = app | ||
|
||
|
||
SOURCES = main.cpp\ | ||
mainwindow.cpp \ | ||
getdatathread.cpp | ||
|
||
HEADERS = mainwindow.h \ | ||
getdatathread.h | ||
|
||
FORMS = mainwindow.ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,59 @@ | ||
#include "getdatathread.h" | ||
|
||
getDataThread::getDataThread(QString url, int request_type, QString data) | ||
{ | ||
this->url = url; | ||
this->request_type = request_type; | ||
this->data = data; | ||
|
||
// nam = new QNetworkAccessManager(this); | ||
// QObject::connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(getDataFinishSlot(QNetworkReply*))); | ||
} | ||
|
||
getDataThread::~getDataThread() | ||
{ | ||
delete reply; | ||
delete nam; | ||
} | ||
|
||
void getDataThread::run() | ||
{ | ||
request_url = QUrl(url); | ||
nam = new QNetworkAccessManager(this); | ||
connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(getDataFinishSlot(QNetworkReply*))); | ||
|
||
if(request_type == 1) | ||
{ | ||
//GETÇëÇó | ||
request.setUrl(request_url); | ||
reply = nam->get(request); | ||
qDebug("getDataThread run\n"); | ||
} | ||
else | ||
{ | ||
//POSTÇëÇó | ||
request.setUrl(request_url); | ||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); | ||
|
||
QByteArray postData(data.toStdString().c_str()); | ||
|
||
reply = nam->post(request,postData); | ||
} | ||
|
||
this->exec(); | ||
} | ||
|
||
void getDataThread::getDataFinishSlot(QNetworkReply *reply) | ||
{ | ||
qDebug("getDataFinishSlot\n"); | ||
|
||
if(reply->error() == QNetworkReply::NoError) | ||
{ | ||
QByteArray bytes = reply->readAll(); | ||
QString str = QString::fromUtf8(bytes); | ||
//ui->textEdit_2->setText(str); | ||
emit getDataThreadFinish(&str); | ||
|
||
qDebug("str[%s]\n",str.toStdString().c_str()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,36 @@ | ||
#ifndef GETDATATHREAD_H | ||
#define GETDATATHREAD_H | ||
|
||
#include <QThread> | ||
#include <QNetworkReply> | ||
#include <QNetworkRequest> | ||
|
||
class getDataThread : public QThread | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
getDataThread(QString url, int request_type, QString data = ""); | ||
~getDataThread(); | ||
|
||
protected: | ||
virtual void run(); | ||
|
||
signals: | ||
void getDataThreadFinish(QString* res); | ||
|
||
public slots: | ||
void getDataFinishSlot(QNetworkReply *reply); | ||
//void replyFinished(QNetworkReply *reply); | ||
|
||
private: | ||
QString url; | ||
int request_type; | ||
QString data; | ||
QUrl request_url; | ||
QNetworkRequest request; | ||
QNetworkReply* reply; | ||
QNetworkAccessManager* nam; | ||
}; | ||
|
||
#endif // GETDATATHREAD_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,14 @@ | ||
#include "mainwindow.h" | ||
#include <QApplication> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
MainWindow w; | ||
|
||
w.setWindowFlags(w.windowFlags()&~Qt::WindowMaximizeButtonHint); | ||
|
||
w.show(); | ||
|
||
return a.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,55 @@ | ||
#include "mainwindow.h" | ||
#include "ui_mainwindow.h" | ||
|
||
#include "getdatathread.h" | ||
|
||
MainWindow::MainWindow(QWidget *parent) | ||
: QMainWindow(parent) | ||
, ui(new Ui::MainWindow) | ||
{ | ||
ui->setupUi(this); | ||
} | ||
|
||
MainWindow::~MainWindow() | ||
{ | ||
delete ui; | ||
} | ||
|
||
void MainWindow::on_pushButton_clicked() | ||
{ | ||
ui->textEdit_2->clear(); | ||
|
||
getDataThread* dataThread = new getDataThread(ui->lineEdit->text(),1); | ||
connect(dataThread, SIGNAL(getDataThreadFinish(QString*)), this, SLOT(showData(QString*))); | ||
dataThread->start(); | ||
} | ||
|
||
void MainWindow::on_pushButton_2_clicked() | ||
{ | ||
ui->textEdit_2->clear(); | ||
|
||
getDataThread* dataThread = new getDataThread(ui->lineEdit->text(), 2, ui->textEdit->toPlainText()); | ||
connect(dataThread, SIGNAL(getDataThreadFinish(QString*)), this, SLOT(showData(QString*))); | ||
dataThread->start(); | ||
|
||
} | ||
|
||
void MainWindow::replyFinished(QNetworkReply *reply) | ||
{ | ||
qDebug("replyFinished\n"); | ||
|
||
if(reply->error() == QNetworkReply::NoError) | ||
{ | ||
QByteArray bytes = reply->readAll(); | ||
QString str = QString::fromUtf8(bytes); | ||
ui->textEdit_2->setText(str); | ||
|
||
qDebug("str[%s]\n",str.toStdString().c_str()); | ||
} | ||
} | ||
|
||
void MainWindow::showData(QString *res) | ||
{ | ||
qDebug("res[%s]\n",(*res).toStdString().c_str()); | ||
ui->textEdit_2->setPlainText(*res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,35 @@ | ||
#ifndef MAINWINDOW_H | ||
#define MAINWINDOW_H | ||
|
||
#include <QMainWindow> | ||
#include <QNetworkReply> | ||
#include <QNetworkRequest> | ||
|
||
#include <qDebug> | ||
|
||
namespace Ui { | ||
class MainWindow; | ||
} | ||
|
||
class MainWindow : public QMainWindow | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit MainWindow(QWidget *parent = 0); | ||
~MainWindow(); | ||
|
||
private slots: | ||
void on_pushButton_clicked(); | ||
|
||
void on_pushButton_2_clicked(); | ||
|
||
void replyFinished(QNetworkReply *reply); | ||
|
||
void showData(QString* res); | ||
|
||
private: | ||
Ui::MainWindow *ui; | ||
}; | ||
|
||
#endif // MAINWINDOW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,144 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>MainWindow</class> | ||
<widget class="QMainWindow" name="MainWindow"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>759</width> | ||
<height>418</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>MainWindow</string> | ||
</property> | ||
<widget class="QWidget" name="centralWidget"> | ||
<widget class="QGroupBox" name="groupBox"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>10</x> | ||
<y>10</y> | ||
<width>741</width> | ||
<height>51</height> | ||
</rect> | ||
</property> | ||
<property name="title"> | ||
<string/> | ||
</property> | ||
<widget class="QWidget" name=""> | ||
<property name="geometry"> | ||
<rect> | ||
<x>11</x> | ||
<y>10</y> | ||
<width>721</width> | ||
<height>30</height> | ||
</rect> | ||
</property> | ||
<layout class="QHBoxLayout" name="horizontalLayout"> | ||
<item> | ||
<widget class="QLabel" name="label"> | ||
<property name="text"> | ||
<string>URL:</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QLineEdit" name="lineEdit"/> | ||
</item> | ||
<item> | ||
<widget class="QPushButton" name="pushButton"> | ||
<property name="minimumSize"> | ||
<size> | ||
<width>93</width> | ||
<height>28</height> | ||
</size> | ||
</property> | ||
<property name="text"> | ||
<string>GET</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QPushButton" name="pushButton_2"> | ||
<property name="text"> | ||
<string>POST</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QLabel" name="label_2"> | ||
<property name="text"> | ||
<string>编码:</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QComboBox" name="comboBox"> | ||
<item> | ||
<property name="text"> | ||
<string>UTF-8</string> | ||
</property> | ||
</item> | ||
<item> | ||
<property name="text"> | ||
<string>GBK</string> | ||
</property> | ||
</item> | ||
</widget> | ||
</item> | ||
</layout> | ||
</widget> | ||
</widget> | ||
<widget class="QGroupBox" name="groupBox_2"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>10</x> | ||
<y>60</y> | ||
<width>741</width> | ||
<height>161</height> | ||
</rect> | ||
</property> | ||
<property name="title"> | ||
<string>参数:</string> | ||
</property> | ||
<widget class="QTextEdit" name="textEdit"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>10</x> | ||
<y>20</y> | ||
<width>721</width> | ||
<height>131</height> | ||
</rect> | ||
</property> | ||
</widget> | ||
</widget> | ||
<widget class="QGroupBox" name="groupBox_3"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>10</x> | ||
<y>230</y> | ||
<width>741</width> | ||
<height>181</height> | ||
</rect> | ||
</property> | ||
<property name="title"> | ||
<string>返回值:</string> | ||
</property> | ||
<widget class="QTextEdit" name="textEdit_2"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>10</x> | ||
<y>20</y> | ||
<width>721</width> | ||
<height>151</height> | ||
</rect> | ||
</property> | ||
</widget> | ||
</widget> | ||
</widget> | ||
</widget> | ||
<layoutdefault spacing="6" margin="11"/> | ||
<resources/> | ||
<connections/> | ||
</ui> |