How to catch incorrect link errors? For example:
ftp://cddis.gsfc.nasa.gov/pub/slr/data/npt_crd/gracea/2010/gracea_20100101.npt.Z
-- this is incorrect link
Why doesn't any error signal work? Or How to determine the correct links? Please, write an example of correct work with errors.
#ifndef WIDGET_H
#define WIDGET_H
#include "Downloader.h"
#include <QWidget>
#include <QNetworkReply>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void onDownloadButtonClicked();
void onSelectTargetFolderButtonClicked();
void onCancelButtonClicked();
void onUpdateProgress(qint64 bytesReceived, qint64 bytesTotal);
void onResult(QNetworkReply *reply);
private:
Ui::Widget *ui;
Downloader m_downloader;
};
#endif // WIDGET_H
Widget.cpp
#include "Widget.h"
#include "ui_Widget.h"
#include <QFileDialog>
#include <QStandardPaths>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->downloadPushButton, &QPushButton::clicked, this, &Widget::onDownloadButtonClicked);
connect(ui->selectTargetFolderPushButton, &QPushButton::clicked, this, &Widget::onSelectTargetFolderButtonClicked);
connect(ui->cancelPushButton, &QPushButton::clicked, this, &Widget::onCancelButtonClicked);
connect(&m_downloader, &Downloader::updateDownloadProgress, this, &Widget::onUpdateProgress);
connect(&m_downloader.manager, &QNetworkAccessManager::finished, this, &Widget::onResult);
}
Widget::~Widget()
{
delete ui;
}
void Widget::onDownloadButtonClicked()
{
m_downloader.get(ui->targetFolderLineEdit->text(), ui->urlLineEdit->text());
}
void Widget::onSelectTargetFolderButtonClicked()
{
QString targetFolder = QFileDialog::getExistingDirectory(this,
tr("Select folder"),
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
ui->targetFolderLineEdit->setText(targetFolder);
}
void Widget::onCancelButtonClicked()
{
m_downloader.cancelDownload();
ui->downloadProgressBar->setMaximum(100);
ui->downloadProgressBar->setValue(0);
}
void Widget::onUpdateProgress(qint64 bytesReceived, qint64 bytesTotal)
{
ui->downloadProgressBar->setMaximum(bytesTotal);
ui->downloadProgressBar->setValue(bytesReceived);
}
void Widget::onResult(QNetworkReply *reply)
{
if (reply->error()) {
qDebug() << "Error";
qDebug() << reply->error();
} else {
qDebug() << "Done";
}
}
Downloader.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QNetworkAccessManager>
#include <QNetworkReply>
//class QNetworkReply;
class QFile;
class Downloader : public QObject
{
Q_OBJECT
using BaseClass = QObject;
public:
explicit Downloader(QObject* parent = nullptr);
bool get(const QString& targetFolder, const QUrl& url);
QNetworkReply* currentReply {nullptr};
QNetworkAccessManager manager;
QString fullName;
public slots:
void cancelDownload();
signals:
void updateDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
private slots:
void onReadyRead();
void onReply(QNetworkReply* reply);
void errorSlot(QNetworkReply::NetworkError er);
private:
QFile* m_file {nullptr};
};
#endif // DOWNLOADER_H
Downloader.cpp
#include "Downloader.h"
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QFile>
#include <QDir>
Downloader::Downloader(QObject* parent) :
BaseClass(parent)
{
connect(&manager, &QNetworkAccessManager::finished, this, &Downloader::onReply);
}
bool Downloader::get(const QString& targetFolder, const QUrl& url)
{
qDebug() << "get";
if (targetFolder.isEmpty() || url.isEmpty())
{
return false;
}
fullName = targetFolder + QDir::separator() + url.fileName();
m_file = new QFile(fullName);
if (!m_file->open(QIODevice::WriteOnly))
{
delete m_file;
m_file = nullptr;
return false;
}
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
currentReply = manager.get(request);
connect(currentReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
this, &Downloader::errorSlot);
connect(currentReply, &QNetworkReply::readyRead, this, &Downloader::onReadyRead);
connect(currentReply, &QNetworkReply::downloadProgress, this, &Downloader::updateDownloadProgress);
return true;
}
void Downloader::onReadyRead()
{
qDebug() << "onReadyRead";
if (m_file)
{
m_file->write(currentReply->readAll());
}
}
void Downloader::cancelDownload()
{
qDebug() << "cancelDownload";
if (currentReply)
{
currentReply->abort();
}
}
void Downloader::onReply(QNetworkReply* reply)
{
qDebug() << "onReply";
if (reply->error() == QNetworkReply::NoError)
{
m_file->flush();
m_file->close();
}
else
{
m_file->remove();
}
delete m_file;
m_file = nullptr;
reply->deleteLater();
}
void Downloader::errorSlot(QNetworkReply::NetworkError er)
{
qDebug() << er;
}
Related
I am trying to redirect my log in a qtextedit windows. I use this post : Redirecting std::cout from DLL in a separate thread to QTextEdit.
Here is my test program :
q_debugstream.h
#ifndef ThreadLogStream_H
#define ThreadLogStream_H
#include <iostream>
#include <streambuf>
#include <string>
#include <QScrollBar>
#include "QTextEdit"
#include "QDateTime"
// MessageHandler
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
class MessageHandler : public QObject
{
Q_OBJECT
public :
MessageHandler(QTextEdit *textEdit, QObject * parent = Q_NULLPTR) : QObject(parent), m_textEdit(textEdit){}
public slots:
void catchMessage(QString msg)
{
this->m_textEdit->append(msg);
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
private:
QTextEdit * m_textEdit;
};
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
class ThreadLogStream : public QObject, public std::basic_streambuf<char>
{
Q_OBJECT
public:
ThreadLogStream(std::ostream &stream, QObject * parent = Q_NULLPTR) :QObject(parent), m_stream(stream)
{
m_old_buf = stream.rdbuf();
stream.rdbuf(this);
}
~ThreadLogStream()
{
// output anything that is left
if (!m_string.empty())
{
emit sendLogString(QString::fromStdString(m_string));
}
m_stream.rdbuf(m_old_buf);
}
protected:
virtual int_type overflow(int_type v)
{
if (v == '\n')
{
emit sendLogString(QString::fromStdString(m_string));
m_string.erase(m_string.begin(), m_string.end());
}
else
m_string += v;
return v;
}
virtual std::streamsize xsputn(const char *p, std::streamsize n)
{
m_string.append(p, p + n);
long pos = 0;
while (pos != static_cast<long>(std::string::npos))
{
pos = static_cast<long>(m_string.find('\n'));
if (pos != static_cast<long>(std::string::npos))
{
std::string tmp(m_string.begin(), m_string.begin() + pos);
emit sendLogString(QString::fromStdString(tmp));
m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
}
}
return n;
}
private:
std::ostream &m_stream;
std::streambuf *m_old_buf;
std::string m_string;
signals:
void sendLogString(const QString& str);
};
#endif // ThreadLogStream_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QTextEdit>
#include "q_debugstream.h"
#include "spdlog/spdlog.h"
namespace Ui {
class MainWindow;
}
class ThreadWorker : public QObject
{
Q_OBJECT
public:
ThreadWorker()
{
}
private:
signals:
void finished();
public slots:
void doWork()
{
std::cout<< "Hello World2" <<std::endl;
qDebug() << "Hello World2q" ;
SPDLOG_INFO("Hello world2 spdlog");
emit finished();
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
// QMessage
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
static void QMessageOutput(QtMsgType , const QMessageLogContext &, const QString &msg);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
public slots:
void SingleThreadTest();
void MultiThreadTest();
private:
Ui::MainWindow *ui;
// MessageHandler for display and ThreadLogStream for redirecting cout
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
MessageHandler *msgHandler = Q_NULLPTR;
ThreadLogStream* m_qd;
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QObject>
#include <QThread>
#include "spdlog/spdlog.h"
// Catch QMessage, redirect to cout
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
void MainWindow::QMessageOutput(QtMsgType , const QMessageLogContext &, const QString &msg)
{
std::cout<<msg.toStdString().c_str()<<std::endl;
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Set up ThreadLogStream, which redirect cout to signal sendLogString
// Set up MessageHandler, wgucg catch message from sendLogString and Display
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
m_qd = new ThreadLogStream(std::cout); //Redirect Console output to QTextEdit
this->msgHandler = new MessageHandler(this->ui->textEdit, this);
connect(m_qd, &ThreadLogStream::sendLogString, msgHandler, &MessageHandler::catchMessage);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
connect(ui->pushButtonSingleThreadTest, SIGNAL(clicked()),this,SLOT(SingleThreadTest()));
connect(ui->pushButtonMultiThreadTest, SIGNAL(clicked()),this,SLOT(MultiThreadTest()));
}
void MainWindow::SingleThreadTest()
{
std::cout<< "Hello World1" <<std::endl;
qDebug() << "Hello World1q" ;
SPDLOG_INFO("Hello world1 spdlog");
}
void MainWindow::MultiThreadTest()
{
QThread *workerThread= new QThread;
ThreadWorker *worker = new ThreadWorker();
worker->moveToThread(workerThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()));
connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
workerThread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "spdlog/spdlog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SPDLOG_INFO("Starting main");
MainWindow w;
w.show();
// Setup QMessageCatch
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
qInstallMessageHandler(MainWindow::QMessageOutput);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
return a.exec();
}
My problem : logs from SPDLOG_INFO appear in my console, but not in my qtextedit window. Obviously, the logs from spdlog are not catch by the function QMessageOutput from qInstallMessageHandler.
My question : Is it possible to catch the log from spdlog on a qtextedit box ?
In spdlog last versions have introduced qt_sinks feature. To use this include the header spdlog/sinks/qt_sinks then call a factory function (spdlog::qt_logger_mt("logger_name", ui->plainTextEdit)) that constructs st or mt(thread-safe) loggers. Also you are not limited to QTextEdit or QPlainTextEdit widgets so you can sink logs into QML, QUdpSocket and any custom widget or QObject based classes with your provided a slot name with a QString parameter.
Usage
custom_class.hpp
#ifndef CUSTOM_CLASS_HPP
#define CUSTOM_CLASS_HPP
#include <QObject>
class custom_class : public QObject
{
Q_OBJECT
public:
explicit custom_class(QObject *parent = nullptr);
public slots:
void log2qml(const QString &str); // slot for using spdlog with custom objects
signals:
void send2qml(const QString& str);
};
#endif // CUSTOM_CLASS_HPP
custom_class.cpp
#include "custom_class.hpp"
custom_class::custom_class(QObject *parent) : QObject(parent)
{
}
void custom_class::log2qml(const QString &str)
{
emit send2qml(str);
}
mainwindow.hpp:
#include "custom_class.hpp"
#include <QMainWindow>
#include <QThread>
#include <spdlog/sinks/qt_sinks.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void log();
private:
Ui::MainWindow *ui;
custom_class c;
QThread *th = nullptr;
std::shared_ptr<spdlog::logger> logger = nullptr, logger_custom = nullptr;
};
#endif // MAINWINDOW_HPP
mainwindow.cpp:
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include "spdlog/spdlog.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// logger write into ui->plainTextEdit
logger = spdlog::qt_logger_mt("plaintextedit_logger", ui->plainTextEdit);
// custom_logger write into qml or any custom object. 3rd param is slot name that passes QString param in custom_object
logger_custom = spdlog::qt_logger_mt("custom_qml_logger", &c, "log2qml");
// set default logger
spdlog::set_default_logger(logger);
log();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::log()
{
th = QThread::create([this] {
for (int i = 0; i < 1000; ++i) {
SPDLOG_INFO(i); // default logger write into ui->plainTextEdit
logger_custom->warn(i); // custom_logger write into qml or any custom object
std::this_thread::sleep_for(std::chrono::microseconds(1));
}
});
th->start();
}
output:
The goal is to display all the application output shown in QtCreator to a QTextEdit as a debug console window so with the same application only those who have password are allowed to see the console window while normal users cannot. There is an exe with several dlls. All std::cout from DLLs and qDebug are needed to be shown in the debug console window.
To achieve this, I have followed
http://www.qtforum.org/article/39768/redirecting-std-cout-std-cerf-qdebug-to-qtextedit.html
The code works great for single thread but is hanged when a thread is started to call functions in DLL. I would like to know how to fix the problem.
code
In mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QObject>
#include <QThread>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_qd = new Q_DebugStream(std::cout,ui->textEdit); //Redirect Console output to QTextEdit
m_qd->Q_DebugStream::registerQDebugMessageHandler(); //Redirect qDebug() output to QTextEdit
connect(ui->pushButtonSingleThreadTest, SIGNAL(clicked()),this,SLOT(SingleThreadTest()));
connect(ui->pushButtonMultiThreadTest, SIGNAL(clicked()),this,SLOT(MultiThreadTest()));
}
void MainWindow::SingleThreadTest()
{
run();
}
void MainWindow::MultiThreadTest()
{
QThread *workerThread= new QThread;
ThreadWorker *worker = new ThreadWorker();
worker->moveToThread(workerThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()));
connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
workerThread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
In mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "q_debugstream.h"
#include "../ToyProj1/Header.h"
namespace Ui {
class MainWindow;
}
class ThreadWorker : public QObject
{
Q_OBJECT
public:
ThreadWorker()
{
}
private:
signals:
void finished();
public slots:
void doWork()
{
run();
emit finished();
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void SingleThreadTest();
void MultiThreadTest();
private:
Ui::MainWindow *ui;
Q_DebugStream* m_qd;
};
#endif // MAINWINDOW_H
in q_debugstream.h
#ifndef Q_DEBUGSTREAM_H
#define Q_DEBUGSTREAM_H
#include <iostream>
#include <streambuf>
#include <string>
#include "QTextEdit.h"
class Q_DebugStream : public std::basic_streambuf<char>
{
public:
Q_DebugStream(std::ostream &stream, QTextEdit* text_edit) : m_stream(stream)
{
log_window = text_edit;
m_old_buf = stream.rdbuf();
stream.rdbuf(this);
}
~Q_DebugStream()
{
m_stream.rdbuf(m_old_buf);
}
static void registerQDebugMessageHandler(){
qInstallMessageHandler(myQDebugMessageHandler);
}
private:
static void myQDebugMessageHandler(QtMsgType, const QMessageLogContext &, const QString &msg)
{
std::cout << msg.toStdString().c_str();
}
protected:
//This is called when a std::endl has been inserted into the stream
virtual int_type overflow(int_type v)
{
if (v == '\n')
{
log_window->append("");
}
return v;
}
virtual std::streamsize xsputn(const char *p, std::streamsize n)
{
QString str(p);
if(str.contains("\n")){
QStringList strSplitted = str.split("\n");
log_window->moveCursor (QTextCursor::End);
log_window->insertPlainText (strSplitted.at(0)); //Index 0 is still on the same old line
for(int i = 1; i < strSplitted.size(); i++){
log_window->append(strSplitted.at(i));
}
}else{
log_window->moveCursor (QTextCursor::End);
log_window->insertPlainText (str);
}
return n;
}
private:
std::ostream &m_stream;
std::streambuf *m_old_buf;
QTextEdit* log_window;
};
#endif // Q_DEBUGSTREAM_H
In DLL,
int run()
{
std::cout << "Hello World" << std::endl;
return 0;
}
The code sample is uploaded to github for reference. Build ToyProj1 and ToyProj1GUI when repeat the problem.
https://github.com/kuwt/ToyProject.git
With the help comments in question, this solution works well with signal and slot mechanism. std::cout and qDebug are redirected to QTextEdit.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
// Setup QMessageCatch
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
qInstallMessageHandler(MainWindow::QMessageOutput);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QTextEdit>
#include "q_debugstream.h"
namespace Ui {
class MainWindow;
}
class ThreadWorker : public QObject
{
Q_OBJECT
public:
ThreadWorker()
{
}
private:
signals:
void finished();
public slots:
void doWork()
{
std::cout<< "Hello World2" <<std::endl;
qDebug() << "Hello World2q" ;
emit finished();
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
// QMessage
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
static void QMessageOutput(QtMsgType , const QMessageLogContext &, const QString &msg);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
public slots:
void SingleThreadTest();
void MultiThreadTest();
private:
Ui::MainWindow *ui;
// MessageHandler for display and ThreadLogStream for redirecting cout
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
MessageHandler *msgHandler = Q_NULLPTR;
ThreadLogStream* m_qd;
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QObject>
#include <QThread>
// Catch QMessage, redirect to cout
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
void MainWindow::QMessageOutput(QtMsgType , const QMessageLogContext &, const QString &msg)
{
std::cout<<msg.toStdString().c_str()<<std::endl;
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Set up ThreadLogStream, which redirect cout to signal sendLogString
// Set up MessageHandler, wgucg catch message from sendLogString and Display
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
m_qd = new ThreadLogStream(std::cout); //Redirect Console output to QTextEdit
this->msgHandler = new MessageHandler(this->ui->textEdit, this);
connect(m_qd, &ThreadLogStream::sendLogString, msgHandler, &MessageHandler::catchMessage);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
connect(ui->pushButtonSingleThreadTest, SIGNAL(clicked()),this,SLOT(SingleThreadTest()));
connect(ui->pushButtonMultiThreadTest, SIGNAL(clicked()),this,SLOT(MultiThreadTest()));
}
void MainWindow::SingleThreadTest()
{
std::cout<< "Hello World1" <<std::endl;
qDebug() << "Hello World1q" ;
}
void MainWindow::MultiThreadTest()
{
QThread *workerThread= new QThread;
ThreadWorker *worker = new ThreadWorker();
worker->moveToThread(workerThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()));
connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
workerThread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
q_debugstream.h
#ifndef ThreadLogStream_H
#define ThreadLogStream_H
#include <iostream>
#include <streambuf>
#include <string>
#include <QScrollBar>
#include "QTextEdit"
#include "QDateTime"
// MessageHandler
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
class MessageHandler : public QObject
{
Q_OBJECT
public :
MessageHandler(QTextEdit *textEdit, QObject * parent = Q_NULLPTR) : QObject(parent), m_textEdit(textEdit){}
public slots:
void catchMessage(QString msg)
{
this->m_textEdit->append(msg);
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
private:
QTextEdit * m_textEdit;
};
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
class ThreadLogStream : public QObject, public std::basic_streambuf<char>
{
Q_OBJECT
public:
ThreadLogStream(std::ostream &stream, QObject * parent = Q_NULLPTR) :QObject(parent), m_stream(stream)
{
m_old_buf = stream.rdbuf();
stream.rdbuf(this);
}
~ThreadLogStream()
{
// output anything that is left
if (!m_string.empty())
{
emit sendLogString(QString::fromStdString(m_string));
}
m_stream.rdbuf(m_old_buf);
}
protected:
virtual int_type overflow(int_type v)
{
if (v == '\n')
{
emit sendLogString(QString::fromStdString(m_string));
m_string.erase(m_string.begin(), m_string.end());
}
else
m_string += v;
return v;
}
virtual std::streamsize xsputn(const char *p, std::streamsize n)
{
m_string.append(p, p + n);
long pos = 0;
while (pos != static_cast<long>(std::string::npos))
{
pos = static_cast<long>(m_string.find('\n'));
if (pos != static_cast<long>(std::string::npos))
{
std::string tmp(m_string.begin(), m_string.begin() + pos);
emit sendLogString(QString::fromStdString(tmp));
m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
}
}
return n;
}
private:
std::ostream &m_stream;
std::streambuf *m_old_buf;
std::string m_string;
signals:
void sendLogString(const QString& str);
};
#endif // ThreadLogStream_H
for long transactions, you can add:
QCoreApplication::processEvents();
in the catchMessage(QString(msg) method of the class MessageHandler in q_debugstream.h, after the append() call.
This updates textEdit 'as soon as possible'.
So, after I make my QJsonModel, load the data, and set the treeView model, it will load the data normally into the treeView. Although, when I do this after a network request has been finished (serviceRequestFinished(QNetworkReply* reply)), it just doesn't woek. I can qDebug the Json data that is passed, but when it comes down to the Json loading and the model setting. It just fails to finish. Also, for some reason it won't set the Json from the network request, but if I just set it to set the json from a separate button, the json get added successfully. So that means that the Json is formatted correctly. The QJsonModel & QJsonItem are external classes not provided with Qt, that located here.
Here is my code:
Mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QModelIndex>
#include <QItemSelection>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QStringList>
#include <QTimer>
#include <QUrl>
#include <QTreeWidgetItem>
#include <QJsonDocument>
#include <QJsonObject>
#include "qjsonmodel.h"
#include <QClipboard>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
void setJson(QString json);
void getPlaylistList(QString accessToken);
QString disableStreams();
void appendEditValues(int currentRow);
void addVectorItems();
void streamCheck();
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_radioButtonNoStream_clicked()
{
streamCheck();
}
void on_radioButtonTwitchStream_clicked()
{
streamCheck();
}
void on_radioButtonYTStream_clicked()
{
streamCheck();
}
void on_pushButtonAddVid_clicked();
void on_pushButtonApplyAddVid_clicked();
void on_pushButtonDeleteSelection_clicked();
void on_pushButtonApplyAll_clicked();
public slots:
void serviceRequestFinished(QNetworkReply* reply);
void connectAPI(QString code);
signals:
void authenticate(QString accessCode);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "authdialog.h"
#include <qDebug>
#include <QVector>
#include <QMessageBox>
#include <QStandardItemModel>
#include <QFile>
#include <QUrlQuery>
int val = 0;
int selected;
QVector<QString> vidTitles;
QVector<QString> vidUrls;
QVector<int> vidNumber;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
streamCheck();
ui->groupBoxEditVideo->setEnabled(false);
connect(this, SIGNAL(authenticate(QString)), SLOT(connectAPI(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::streamCheck()
{
if (ui->radioButtonNoStream->isChecked())
{
ui->groupBoxStreamSetup->setEnabled(false);
ui->groupBoxStreamSetup->setTitle("No Stream Selected!");
}
else if (ui->radioButtonTwitchStream->isChecked())
{
ui->groupBoxStreamSetup->setEnabled(true);
ui->groupBoxStreamSetup->setTitle("Twitch Setup");
}
else if (ui->radioButtonYTStream->isChecked())
{
ui->groupBoxStreamSetup->setEnabled(true);
ui->groupBoxStreamSetup->setTitle("YouTube Stream Setup");
}
}
void MainWindow::on_pushButtonAddVid_clicked()
{
AuthDialog *auth = new AuthDialog(this);
auth->setModal(true);
auth->exec();
delete auth;
}
void MainWindow::setJson(QString json)
{
qDebug() << json;
QJsonModel * model = new QJsonModel;
model->loadJson(json.toUtf8());
ui->treeView->setModel(model);
delete model;
}
void MainWindow::getPlaylistList(QString accessToken)
{
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
//
QNetworkRequest request(QUrl("https://www.googleapis.com/youtube/v3/search?part=snippet&forMine=true&order=viewCount&type=video&access_token=" + accessToken));
networkManager->get(request);
}
QString MainWindow::disableStreams()
{
QString line = "";
QString test = "";
QFile file;
file.setFileName("C:/WampStack/apache2/htdocs/index.html");
file.open(QIODevice::ReadWrite);
QTextStream in(&file);
while(!in.atEnd())
{
line += in.readAll();
}
if (line.contains("<!--t-->") && !line.contains("<!--y-->"))
{
//Twitch Stream is on, must turn it off
test = line.replace("<!--t-->", "<!--t");
test = test.replace("><!--tt-->", ">tt-->");
return test;
}
else if (line.contains("<!--y-->") && !line.contains("<!--t-->"))
{
//YouTube Stream is on, must turn it off
test = line.replace("<!--y-->", "<!--y");
test = test.replace("><!--yy-->", ">yy-->");
return test;
}
else if (line.contains("<!--y-->") && line.contains("<!--t-->"))
{
test = line.replace("<!--y-->", "<!--y");
test = test.replace("><!--yy-->", ">yy-->");
test = test.replace("<!--t-->", "<!--t");
test = test.replace("><!--tt-->", ">tt-->");
return test;
}
else
{
return line;
}
file.close();
return line;
}
void MainWindow::connectAPI(QString code)
{
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
QUrlQuery postData;
postData.addQueryItem("code", code);
postData.addQueryItem("client_id", "CLIENT-ID");
postData.addQueryItem("client_secret", "CLIENT-SECRET");
postData.addQueryItem("redirect_uri", "REDIRECT-URI");
postData.addQueryItem("grant_type", "authorization_code");
QNetworkRequest request(QUrl("https://accounts.google.com/o/oauth2/token"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
networkManager->post(request, postData.toString(QUrl::FullyEncoded).toUtf8());
}
void MainWindow::appendEditValues(int currentRow)
{
}
void MainWindow::addVectorItems()
{
}
void MainWindow::on_pushButtonApplyAddVid_clicked()
{
}
void MainWindow::on_pushButtonDeleteSelection_clicked()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Are you sure?", "Do you want to permanently delete this entry?", QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes)
{
}
addVectorItems();
}
void MainWindow::on_pushButtonApplyAll_clicked()
{
QString line = disableStreams();
QFile::remove("C:/WampStack/apache2/htdocs/index.html");
QFile file;
file.setFileName("C:/WampStack/apache2/htdocs/index.html");
file.open(QIODevice::ReadWrite);
//Start code for streaming
if (ui->radioButtonTwitchStream->isChecked() && line.contains("<!--t") && line.contains(">tt-->") && !line.contains("<!--t-->") && !line.contains("<!--tt-->"))
{
QString test = line.replace("<!--t", "<!--t-->");
test = line.replace(">tt-->", "><!--tt-->");
QTextStream stream( &file );
stream << test;
}
else if (ui->radioButtonYTStream->isChecked() && line.contains("<!--y") && line.contains(">yy-->") && !line.contains("<!--y-->") && !line.contains("<!--yy-->"))
{
QString test = line.replace("<!--y", "<!--y-->");
test = test.replace(">yy-->", "><!--yy-->");
QTextStream stream( &file );
stream << test;
}
else if (ui->radioButtonNoStream->isChecked())
{
QTextStream stream(&file);
stream << line;
}
//End code for streaming
file.close();
}
void MainWindow::serviceRequestFinished(QNetworkReply* reply)
{
QByteArray json = reply->readAll();
QString output = QString::fromUtf8(json);
QJsonDocument settdoc = QJsonDocument::fromJson(output.toUtf8());
QJsonObject sett2 = settdoc.object();
if (val == 0)
{
val++;
getPlaylistList(sett2.value(QString("access_token")).toString());
}
else if (val == 1)
{
setJson(output.toUtf8());
}
}
Main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
QJsonModel only works if the the JSON's root element is an object (see the bug report on github.com). I guess your JSON's root is an array.
I'm developing a server that responds multiple connections from a client. But I have a problem where I have not found any solution.
I created a class (TcpSocket) for QTcpSocket, which separates the signals and slots for each connection.
But my server is not recognizing the signal of this class, it looks for QTcpSocket, and not by TcpSocket.
I'll post the code, maybe you understand better, because my English is not good.
tcpsocket.h
#ifndef TCPSOCKET_H
#define TCPSOCKET_H
#include <QObject>
#include <QtNetwork>
class TcpSocket: public QTcpSocket
{
Q_OBJECT
QTcpSocket *Socket;
public:
TcpSocket (QTcpSocket *);
virtual ~TcpSocket();
public slots:
void slotReadyRead();
void slotConnected();
void slotDisconnected();
signals:
void dataReady (TcpSocket *sckt);
void newConnection(TcpSocket *sckt);
void lostConnection(TcpSocket *sckt);
};
#endif // TCPSOCKET_H
tcpsocket.cpp
#include "tcpsocket.h"
TcpSocket::TcpSocket(QTcpSocket * socket)
: Socket (socket)
{
connect(Socket, SIGNAL(readyRead()), this, SLOT (slotReadyRead()));
connect(Socket, SIGNAL(connected()), this, SLOT(slotConnected()));
connect(Socket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
}
TcpSocket::~TcpSocket()
{
}
void TcpSocket::slotReadyRead()
{
emit dataReady(this);
}
void TcpSocket::slotConnected()
{
emit newConnection(this);
}
void TcpSocket::slotDisconnected()
{
emit lostConnection(this);
}
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include "custom/player.h"
#include "events.h"
#include "socketmanager.h"
#include "tcpsocket.h"
#include <QMessageBox>
namespace Ui {
class Server;
}
class Server : public QMainWindow
{
Q_OBJECT
public:
explicit Server(QWidget *parent = 0);
int max_connections;
~Server();
private slots:
...
void client_Disconnected(TcpSocket *socket);
void client_SendedBytes(qint64 bytes);
void client_GetBytes(TcpSocket* socket);
...
private:
Ui::Server *ui;
QTcpServer *server;
QList<TcpSocket *> client;
...
};
#endif // SERVER_H
server.cpp
int j; // connection count
void Server::server_Connected()
{
client.insert(j, (TcpSocket*)server->nextPendingConnection());
TcpSocket *sckt = client[j];
// error:
connect(sckt, SIGNAL(newConnection(TcpSocket*)), this, SLOT(client_GetBytes(TcpSocket*)));
connect(sckt, SIGNAL(lostConnection(TcpSocket*)), this, SLOT(client_Disconnected(TcpSocket*)));
QByteArray block;
QTextStream out(&block, QIODevice::WriteOnly);
out << "accepted";
ui->log->append(QString("Host connected: %1, index %2").arg(sckt->localAddress().toString()).arg(j));
std::string stdString = "accepted";
QByteArray byteArray(stdString.c_str(), stdString.length());
qint64 len = sckt->write(byteArray);
if(len != byteArray.size())
ui->log->append("Error!");
sckt->flush();
j++;
}
void Server::client_Disconnected(TcpSocket *socket)
{
...
}
void Server::client_GetBytes(TcpSocket *socket)
{
...
}
The error:
QObject::connect: No such signal QTcpSocket::newConnection(TcpSocket*) in ..\SOLEditorServer\server.cpp:45
QObject::connect: (receiver name: 'Server')
QObject::connect: No such signal QTcpSocket::lostConnection(TcpSocket*) in ..\SOLEditorServer\server.cpp:46
QObject::connect: (receiver name: 'Server')
also full code (server)
#include "server.h"
#include "ui_server.h"
#include <QHostAddress>
Server::Server(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Server)
{
max_connections = 20;
SM = new SocketManager();
ui->setupUi(this);
server = new QTcpServer(this);
connect(server, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(server_Error()));
connect(server, SIGNAL(newConnection()), this, SLOT(server_Connected()));
event = new Events(ui->log);
setWindowFlags(Qt::WindowCloseButtonHint);
}
Server::~Server()
{
delete ui;
}
void Server::on_pushButton_clicked()
{
QHostAddress host;
host.setAddress(ui->txt_ip->text());
server->listen(host, ui->txt_port->value());
ui->log->append("Connected!");
}
void Server::server_Error()
{
ui->log->append(server->errorString());
}
int j;
void Server::server_Connected()
{
client.insert(j, (TcpSocket*)server->nextPendingConnection());
TcpSocket *sckt = client[j];
connect(sckt, SIGNAL(newConnection(TcpSocket*)), this, SLOT(client_GetBytes(TcpSocket*)));
connect(sckt, SIGNAL(lostConnection(TcpSocket*)), this, SLOT(client_Disconnected(TcpSocket*)));
QByteArray block;
QTextStream out(&block, QIODevice::WriteOnly);
out << "accepted";
ui->log->append(QString("Host connected: %1, index %2").arg(sckt->localAddress().toString()).arg(j));
std::string stdString = "accepted";
QByteArray byteArray(stdString.c_str(), stdString.length());
qint64 len = sckt->write(byteArray);
if(len != byteArray.size())
ui->log->append("Error!");
sckt->flush();
j++;
}
void Server::client_Disconnected(TcpSocket *socket)
{
ui->log->append(QString("Desconectado. (%1)").arg(socket->localAddress().toString()));
client.removeAt(client.indexOf(socket));
}
void Server::client_SendedBytes(qint64 bytes)
{
QString dataxD = QString::number(bytes);
ui->log->append(QString("%1 bytes enviados.").arg(dataxD));
}
void Server::client_GetBytes(TcpSocket *socket)
{
QByteArray buffer;
buffer.resize(socket->bytesAvailable());
socket->read(buffer.data(), buffer.size());
QString data(buffer);
if(data.startsWith("REGISTER "))
{
QString received = data.split("REGISTER ")[1];
ui->log->append(received);
QString user = received.split(":")[0];
QString key = received.split(":")[1];
playerList.append(user);
playerKey.append(key);
event->eventNewPlayer(user);
SM->sendPacketToAll(client, QString("GREETING %1").arg(user));
} else if(data.startsWith("CHAT("))
{
QString UserData = data.split("CHAT(")[1].split(")")[0];
if(!checkUser(UserData))
{
ui->log->append("Username without a valid hash!");
return;
}
QString User = getUsernameFromData(UserData);
QString Message = data.split(QString("CHAT(%1) ").arg(UserData))[1];
event->eventNewChatmessage(Message, User);
SM->sendPacketToAll(client, QString("CHAT(%1) %2").arg(User).arg(Message));
}
}
bool Server::checkUser(QString usernamedata)
{
if(!usernamedata.contains(":"))
return false;
QString username = usernamedata.split(":")[0];
QString key = usernamedata.split(":")[1];
if(!playerList.contains(username) || !playerKey.contains(key))
return false;
int playerIndex = playerList.indexOf(username);
QString hashFromList = playerKey[playerIndex];
if(hashFromList != key)
return false;
return true;
}
bool Server::checkUser2(QString username, QString key)
{
if(!playerList.contains(username) || !playerKey.contains(key))
return false;
int playerIndex = playerList.indexOf(username);
QString hashFromList = playerKey[playerIndex];
if(hashFromList != key)
return false;
return true;
}
QString Server::getUsernameFromData(QString usernamedata)
{
if(!usernamedata.contains(":"))
return "Unknow";
QString username = usernamedata.split(":")[0];
return username;
}
QString Server::getUserkeyFromData(QString usernamedata)
{
if(!usernamedata.contains(":"))
return "Unknow";
QString key = usernamedata.split(":")[1];
return key;
}
void Server::on_pushButton_2_clicked()
{
/*std::string stdString = "BANNED";
QByteArray byteArray(stdString.c_str(), stdString.length());
clientConnection->write(byteArray);*/
}
full server.h
#ifndef SERVER_H
#define SERVER_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include "custom/player.h"
#include "events.h"
#include "socketmanager.h"
#include "tcpsocket.h"
#include <QMessageBox>
namespace Ui {
class Server;
}
class Server : public QMainWindow
{
Q_OBJECT
public:
explicit Server(QWidget *parent = 0);
int max_connections;
~Server();
private slots:
void on_pushButton_clicked();
void server_Error();
void server_Connected();
void client_Disconnected(TcpSocket *socket);
void client_SendedBytes(qint64 bytes);
void client_GetBytes(TcpSocket* socket);
void on_pushButton_2_clicked();
bool checkUser(QString usernamedata);
bool checkUser2(QString username, QString key);
QString getUsernameFromData(QString usernamedata);
QString getUserkeyFromData(QString usernamedata);
private:
Ui::Server *ui;
QTcpServer *server;
QList<TcpSocket *> client;
QStringList playerList;
QStringList playerKey;
Events *event;
SocketManager *SM;
};
#endif // SERVER_H
Fixed!
Modifications:
class TcpSocket: public QTcpSocket // old
class TcpSocket: public QObject // new
// old
void dataReady (TcpSocket *sckt);
void newConnection(TcpSocket *sckt);
void lostConnection(TcpSocket *sckt);
// new
void dataReady(QTcpSocket *sckt);
void newConnection(QTcpSocket *sckt);
void lostConnection(QTcpSocket *sckt);
// old
TcpSocket::TcpSocket(QTcpSocket * socket)
: Socket (socket)
{
connect(Socket, SIGNAL(readyRead()), this, SLOT (slotReadyRead()));
connect(Socket, SIGNAL(connected()), this, SLOT(slotConnected()));
connect(Socket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
}
// new
TcpSocket::TcpSocket (QTcpSocket * socket)
: Socket (socket)
{
this->sock = socket;
connect(socket, SIGNAL(readyRead()), this, SLOT (slotReadyRead()));
connect(socket, SIGNAL(connected()), this, SLOT(slotConnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
}
// old
void TcpSocket::slotReadyRead()
{
emit dataReady(this);
}
void TcpSocket::slotConnected()
{
emit newConnection(this);
}
void TcpSocket::slotDisconnected()
{
emit lostConnection(this);
}
// new
void TcpSocket::slotReadyRead()
{
emit dataReady(this->sock);
}
void TcpSocket::slotConnected()
{
emit newConnection(this->sock);
}
void TcpSocket::slotDisconnected()
{
emit lostConnection(this->sock);
}
// old
public:
TcpSocket (QTcpSocket *);
virtual ~TcpSocket();
// new
public:
TcpSocket (QTcpSocket *);
QTcpSocket *sock;
Done, now just use connect() function, like this:
QTcpSocket *socket = (your QTcpSocket);
TcpSocket *tcpSocket = new TcpSocket(socket);
connect(tcpSocket, SIGNAL(dataReady(QTcpSocket*)), this, SLOT(your_slot(QTcpSocket*)));
You really helped me, and sorry. Good luck, if anyone has the same problem that I had (not solved), please contact me!
I want to get string that appears on following link in my Qt program how can I get it ?
http://en.wikipedia.org/w/api.php?action=opensearch&search=centaurus_constellation
I have coded following files to get string appearing on above page in m_DownloadedData, but it is storing empty string at the end of the program , can someone please help me get data from above link ?
.h file:
#ifndef SKYOBJDESCRIPTION_H
#define SKYOBJDESCRIPTION_H
#include <QObject>
#include <QByteArray>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
class SkyObjDescription : public QObject
{
Q_OBJECT
public:
explicit SkyObjDescription(const QString soName, const QString soType, QObject* parent = 0);
virtual ~SkyObjDescription();
QByteArray downloadedData() const;
signals:
void downloaded();
private slots:
void fileDownloaded(QNetworkReply* reply);
private:
QString soName,soType;
QByteArray m_DownloadedData;
};
#endif // SKYOBJDESCRIPTION_H
and
.cpp file
#include <QString>
#include <QUrl>
#include <QDebug>
#include "skyobjdescription.h"
SkyObjDescription::SkyObjDescription(const QString so_Name, const QString so_Type, QObject* parent): soName(so_Name), soType(so_Type), QObject(parent)
{
QString wikiLink = "http://en.wikipedia.org/w/api.php?action=opensearch&search="+ soName.replace(" ", "_").toLower() + "_" + soType.toLower() + "&format=xml&limit=1.xml";
// QUrl wikiUrl("http://en.wikipedia.org/w/api.php?action=opensearch&search=hello_world&format=xml&limit=1.xml");
m_DownloadedData = wikiUrl.toEncoded();
qDebug() << "wikiUrl.toEncoded(): " << m_DownloadedData;
QNetworkRequest req(wikiUrl);
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(fileDownloaded(QNetworkReply*)));
manager->get(req);
}
SkyObjDescription::~SkyObjDescription()
{
}
void SkyObjDescription::fileDownloaded(QNetworkReply* reply)
{
m_DownloadedData = reply->readAll();
qDebug() << "received reply";
qDebug() << m_DownloadedData;
reply->deleteLater();
emit downloaded();
}
QByteArray SkyObjDescription::downloadedData() const
{
qDebug() << m_DownloadedData;
return m_DownloadedData;
}
part of main.cpp
SkyObjDescription * skd = new SkyObjDescription(m_Name, "Constellation");
QString data(skd->downloadedData());
qDebug() << data;
delete skd;
If there is other way to get data from link please explain it.
Thank you very much :)
What you probably experiment is caused by your double call on QNetworkReply::readAll.
Remembers it is a IO operation, and there is no way to read multiple times the information contain by the netwrk reply.
Just comment your debug line :
void SkyObjDescription::fileDownloaded(QNetworkReply* pReply)
{
//qDebug() << pReply->readAll();
m_DownloadedData = pReply->readAll();
//emit a signal
pReply->deleteLater();
emit downloaded();
}
edit (for completness)
The test code I use :
#ifndef TMP_H
#define TMP_H
#include <QObject>
class QNetworkReply;
class Tmp : public QObject
{
Q_OBJECT
public:
explicit Tmp(QObject *parent = 0);
signals:
public slots:
void displayResult(QNetworkReply* reply);
};
#endif // TMP_H
//tmp.cpp
#include "tmp.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QDebug>
Tmp::Tmp(QObject *parent) :
QObject(parent)
{
QUrl url("http://en.wikipedia.org/wiki/Centaurus_constellation");
QNetworkRequest req(url);
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(displayResult(QNetworkReply*)));
manager->get(req);
}
void Tmp::displayResult(QNetworkReply *reply) {
QByteArray buffer = reply->readAll();
qDebug() << "received reply";
qDebug() << buffer;
reply->deleteLater();
}