Advertisement
czaffik

qt QNetworkAccesManager

Dec 18th, 2016
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. Plik main.cpp:
  2. #include "mainwindow.h"
  3. #include <QApplication>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     QApplication a(argc, argv);
  8.     MainWindow w;
  9.     w.show();
  10.  
  11.     return a.exec();
  12. }
  13.  
  14. Plik mainwindow.h:
  15. #ifndef MAINWINDOW_H
  16. #define MAINWINDOW_H
  17.  
  18. #include <QMainWindow>
  19. #include <QString>
  20. #include <QtNetwork/QNetworkAccessManager>
  21. #include <memory>
  22.  
  23. namespace Ui {
  24. class MainWindow;
  25. }
  26.  
  27. class MainWindow : public QMainWindow
  28. {
  29.     Q_OBJECT
  30.  
  31. public:
  32.     explicit MainWindow(QWidget *parent = 0);
  33.     ~MainWindow();
  34.  
  35. private slots:
  36.     void urlConnect();
  37.     void urlRead();
  38.     void urlFinished();
  39.     void urlSend();
  40.     void sendFinished(QNetworkReply *r);
  41.  
  42. private:
  43.     std::unique_ptr<Ui::MainWindow> ui;
  44.     QNetworkAccessManager *manager;
  45.     QNetworkReply *reply;
  46.     QString text;
  47.     QString notify;
  48.     QByteArray buffer;
  49. };
  50.  
  51. #endif // MAINWINDOW_H
  52.  
  53. Plik mainwindow.cpp:
  54. #include "mainwindow.h"
  55. #include "ui_mainwindow.h"
  56. #include <QtNetwork/QNetworkRequest>
  57. #include <QtNetwork/QNetworkReply>
  58.  
  59. MainWindow::MainWindow(QWidget *parent) :
  60.     QMainWindow(parent),
  61.     ui(new Ui::MainWindow)
  62. {
  63.     ui->setupUi(this);
  64.     manager = new QNetworkAccessManager;
  65.  
  66.     connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(urlConnect()));
  67.     connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(urlSend()));
  68. }
  69.  
  70. MainWindow::~MainWindow()
  71. {
  72.     delete manager;
  73. }
  74.  
  75. void MainWindow::urlConnect()
  76. {
  77.     notify = QString("");
  78.     QUrl url = ui->lineEdit->text();
  79.     QNetworkRequest req(url);
  80.     notify.append(QString("host: ") + req.url().host() + QString("\n"));
  81.     notify.append(QString("path: ") + req.url().path() + QString("\n"));
  82.     notify.append(QString("user: ") + req.url().userName() + QString("\n"));
  83.     notify.append(QString("user: ") + req.url().userInfo() + QString("\n"));
  84.     notify.append(QString("url: ") + req.url().url() + QString("\n"));
  85.     notify.append(QString("str: ") + req.url().toString() + QString("\n"));
  86.     notify.append(QString("file: ") + req.url().toLocalFile() + QString("\n"));
  87.  
  88.     reply = manager->get(req);
  89.     text = QString("czytanie danych");
  90.     ui->label_2->setText(text);
  91.     connect(reply, SIGNAL(readyRead()), this, SLOT(urlRead()));
  92.     connect(reply, SIGNAL(finished()), this, SLOT(urlFinished()));
  93.  
  94.     ui->textBrowser->setText(notify);
  95. }
  96.  
  97. void MainWindow::urlRead()
  98. {
  99.     text.append(".");
  100.     if (text.length() > 25) text = QString("czytanie danych");
  101.     ui->label_2->setText(text);
  102.     buffer += reply->readAll();
  103. }
  104.  
  105. void MainWindow::urlFinished()
  106. {
  107.     text = QString("wczytano dane");
  108.     ui->label_2->setText(text);
  109.     QString a(buffer);
  110.     notify.append(QString("dane: ") + a);
  111.     ui->textBrowser->setText(notify);
  112. }
  113.  
  114. void MainWindow::urlSend()
  115. {
  116.     QString txt = ui->textEdit->toPlainText();
  117.     QByteArray postData;
  118.     postData.append(txt);
  119.     QNetworkRequest request = QNetworkRequest(QUrl(ui->lineEdit->text()));
  120.     request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
  121.     manager->post(request, postData);
  122.     connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(sendFinished(QNetworkReply*)));
  123. }
  124.  
  125. void MainWindow::sendFinished(QNetworkReply *r)
  126. {
  127.     disconnect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(sendFinished(QNetworkReply*)));
  128.     if(r->error() == QNetworkReply::NoError)
  129.         ui->textBrowser_2->setText("wysyłanie zakończone bez błędu");
  130.     else
  131.         ui->textBrowser_2->setText(QString("Błąd przy wysyłaniu: ") + r->errorString());
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement