Hurz::Hurz()
{
moveToThread(this);
}
Hurz::Hurz()
{
moveToThread(this);
}
To copy to clipboard, switch view to plain text mode
Some example (it crashed before using moveToThread)
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QCoreApplication>
#include <QThread>
#include <QEventLoop>
#include <QDebug>
#include <QTime>
{
Q_OBJECT
public slots:
void dataAvailable();
void slot_finished();
public:
Hurz();
void run();
private:
QNetworkAccessManager *_manager;
QNetworkReply *_reply;
QNetworkRequest *_request;
};
Hurz::Hurz()
{
moveToThread(this);
m_time
= QTime::currentTime();
}
void Hurz::run()
{
qDebug
() <<
"Hurz::run thread id is " <<
QThread::currentThreadId();
_manager = new QNetworkAccessManager();
// you need to change the url to a different one which sends you "endless" data, eg. a file with almost gigabytes of data
_request
= new QNetworkRequest
(QUrl("http://ftp.stack.nl/pub/users/dimitri/doxygen-1.6.2-setup.exe"));
_reply = _manager->get(*_request);
connect(_reply, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
connect(_reply, SIGNAL(finished()), this, SLOT(slot_finished()));
exec();
}
void Hurz::dataAvailable()
{
if (_reply->size() > 100000) {
qDebug
() <<
"Hurz::dataAvailable: " <<
"Thread id is" <<
QThread::currentThreadId();
int ms
= m_time.
msecsTo(QTime::currentTime());
m_time
= QTime::currentTime();
qDebug() << " " << arr.size() << "in" << ms << "ms. " << (float)arr.size() / 1024 / ms * 1000 << "kB/s";
}
}
void Hurz::slot_finished()
{
qDebug
() <<
"Hurz::slot_finished: " <<
"Thread id is" <<
QThread::currentThreadId();
if (_reply->size() > 0) {
int ms
= m_time.
msecsTo(QTime::currentTime());
m_time
= QTime::currentTime();
qDebug() << " " << arr.size() << "in" << ms << "ms. " << (float)arr.size() / 1024 / ms * 1000 << "kB/s";
}
quit();
}
int main(int argc, char **argv)
{
Hurz h;
//QEventLoop evLoop;
QObject::connect(&h,
SIGNAL(finished
()),
&app,
SLOT(quit
()));
h.start();
//for(;;)
// evLoop.processEvents(QEventLoop::ExcludeUserInputEvents);
//evLoop.exec(QEventLoop::ExcludeUserInputEvents);
app.exec();
}
#include "main.moc"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QCoreApplication>
#include <QThread>
#include <QEventLoop>
#include <QDebug>
#include <QTime>
class Hurz : public QThread
{
Q_OBJECT
public slots:
void dataAvailable();
void slot_finished();
public:
Hurz();
void run();
private:
QNetworkAccessManager *_manager;
QNetworkReply *_reply;
QNetworkRequest *_request;
QTime m_time;
};
Hurz::Hurz()
{
moveToThread(this);
m_time = QTime::currentTime();
}
void Hurz::run()
{
qDebug() << "Hurz::run thread id is " << QThread::currentThreadId();
_manager = new QNetworkAccessManager();
// you need to change the url to a different one which sends you "endless" data, eg. a file with almost gigabytes of data
_request = new QNetworkRequest(QUrl("http://ftp.stack.nl/pub/users/dimitri/doxygen-1.6.2-setup.exe"));
_reply = _manager->get(*_request);
connect(_reply, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
connect(_reply, SIGNAL(finished()), this, SLOT(slot_finished()));
exec();
}
void Hurz::dataAvailable()
{
if (_reply->size() > 100000) {
qDebug() << "Hurz::dataAvailable: " << "Thread id is" << QThread::currentThreadId();
QByteArray arr = _reply->readAll();
int ms = m_time.msecsTo(QTime::currentTime());
m_time = QTime::currentTime();
qDebug() << " " << arr.size() << "in" << ms << "ms. " << (float)arr.size() / 1024 / ms * 1000 << "kB/s";
}
}
void Hurz::slot_finished()
{
qDebug() << "Hurz::slot_finished: " << "Thread id is" << QThread::currentThreadId();
if (_reply->size() > 0) {
QByteArray arr = _reply->readAll();
int ms = m_time.msecsTo(QTime::currentTime());
m_time = QTime::currentTime();
qDebug() << " " << arr.size() << "in" << ms << "ms. " << (float)arr.size() / 1024 / ms * 1000 << "kB/s";
}
quit();
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
Hurz h;
//QEventLoop evLoop;
QObject::connect(&h, SIGNAL(finished()), &app, SLOT(quit()));
h.start();
//for(;;)
// evLoop.processEvents(QEventLoop::ExcludeUserInputEvents);
//evLoop.exec(QEventLoop::ExcludeUserInputEvents);
app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks