[Qt-interest] QThread and QNetwork
Matias Hernandez Arellano
msdark at archlinux.cl
Thu May 26 18:56:15 CEST 2011
(sorry for my english)
Hi, i'm trying do make an application to query a couchdb database using HTTP request to the _change API of the database.
To do this i create a class (DBCheck) that make a request to the _change API using the continuos feed, this is a HTTP connection that live forever and make possible to notify the client when a change occur in DB (real time, like Push notification).
Then i create another class (just for test) no show a notification (in console) of the change.
I connect this two classes using Signals and Slots.
Now i need to make this work with a GUI, the idea is show in a part of the GUI a "table" that auto--update any change in DB (when DB notify for changes)..
To do this i thought i need to use Threads, so for testing purpose i create a console application and try to run this, but i can't make that the Http class (DBCheck) run inside a thread an notify every change to the main thread (or other object using signals) .. i get an error about the diferents parents of the object..
So this is the code (without thread)
#ifndef DBCHECK_H
#define DBCHECK_H
#include <QNetworkAccessManager>
#include <QUrl>
#include <QObject>
#include <QByteArray>
#include <qjson/parser.h>
#include <QVariantMap>
#include <QDebug>
class QNetworkReply;
class DBCheck: public QObject
{
Q_OBJECT
public:
DBCheck();
void startRequest(QUrl);
signals:
void change();
private slots:
void httpFinished();
void httpReadyRead();
private:
QUrl url;
QNetworkAccessManager qnam;
QNetworkReply *reply;
};
DBCheck::DBCheck()
{
}
void DBCheck::startRequest(QUrl url){
reply = qnam.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()),this, SLOT(httpFinished()));
connect(reply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
return;
}
void DBCheck::httpFinished(){
qDebug()<<"Finished";
return;
}
void DBCheck::httpReadyRead(){
this->result = this->reply->readAll();
emit change();
}
//the other class
#ifndef NOTIF_H
#define NOTIF_H
#include <QObject>
#include <QDebug>
class Notif:public QObject{
Q_OBJECT
public:
Notif(){
}
~Notif(){
}
public slots:
void notificar(){
qDebug()<<"Cambio!";
}
};
#endif // NOTIF_H
and the main.cpp
#include <QCoreApplication>
#include "dbcheck.h"
#include "notif.h"
#include <QDebug>
int main(int argc, char *argv[]){
QCoreApplication app(argc, argv);
DBCheck *db = new DBCheck();
Notif *t = new Notif();
QObject::connect(db,SIGNAL(change()),t,SLOT(notificar()));
db->startRequest(QUrl("http://127.0.0.1:5984/test/_changes?feed=continuous&since=39"));
return app.exec();
}
How can i run the DBCheck object in a thread???
any guideline or idea would very appreciate ...
Thanks in advance.
Matías Hernandez Arellano
Ingeniero de Software/Proyectos en VisionLabs S.A
CDA Archlinux-CL
www.msdark.archlinux.cl
More information about the Qt-interest-old
mailing list