Hi (sorry for my english).. i'm new in the forum, and this is my first post,(shame.. is a question) .. I'm working in a console application that use OpenGL to show a Camera view, and i add Qt to use the really awesome class QThread (C++) ..But i have some problems.. here my code and then the description:
#ifndef CONTADOR_REGISTER_H
#define CONTADOR_REGISTER_H
#include <QThread>
#include <QObject>
#include <QDebug>
#include "database.h"
class ContadorRegister
: public QThread{
Q_OBJECT
public:
ContadorRegister(User user, UserID id);
void run();
private:
User user;
UserID uid;
public slots:
void fin(){ qDebug()<<"Thread ended"; }
};
#endif // CONTADOR_REGISTER_H
//And the implementation in contador_register.cpp
#include "contador_register.h"
ContadorRegister::ContadorRegister(const User userG, UserID id){
this->uid = id;
this->user = userG;
}
void ContadorRegister::run(){
this->setTerminationEnabled(true);
QObject::connect(this,
SIGNAL(finished
()),
this,
SLOT(fin
()));
int dir=0;
//Some calculations
//Registro en BD
if(dir==1){
QString sql
="INSERT INTO registro VALUES(";
sql.append("NOW())");
DB->insertar(sql);
}
this->finished();
}
#ifndef CONTADOR_REGISTER_H
#define CONTADOR_REGISTER_H
#include <QThread>
#include <QObject>
#include <QDebug>
#include "database.h"
class ContadorRegister : public QThread
{
Q_OBJECT
public:
ContadorRegister(User user, UserID id);
void run();
private:
User user;
UserID uid;
public slots:
void fin(){ qDebug()<<"Thread ended"; }
};
#endif // CONTADOR_REGISTER_H
//And the implementation in contador_register.cpp
#include "contador_register.h"
ContadorRegister::ContadorRegister(const User userG, UserID id){
this->uid = id;
this->user = userG;
}
void ContadorRegister::run(){
this->setTerminationEnabled(true);
QObject::connect(this,SIGNAL(finished()),this,SLOT(fin()));
int dir=0;
//Some calculations
//Registro en BD
if(dir==1){
QString sql="INSERT INTO registro VALUES(";
sql.append("NOW())");
DB->insertar(sql);
}
this->finished();
}
To copy to clipboard, switch view to plain text mode
And finally the function where i call this thread.
This is a callback function .. this function trigger up every time of a user is saw for the camera.
ContadorRegister* cr = new ContadorRegister(generator,nId);
cr->start();
qDebug()<<"Starting" << cr->currentThreadId();
if(cr->isRunning()){
qDebug()<<"Still Running "<<cr->currentThreadId();
}else
{
qDebug()<<"Ended "<<cr->currentThreadId();
}
ContadorRegister* cr = new ContadorRegister(generator,nId);
cr->start();
qDebug()<<"Starting" << cr->currentThreadId();
if(cr->isRunning()){
qDebug()<<"Still Running "<<cr->currentThreadId();
}else
{
qDebug()<<"Ended "<<cr->currentThreadId();
}
To copy to clipboard, switch view to plain text mode
Well .. the problem is.. i this function (or another in the main) i need to check if the QThread is running or not...
And beside.. if the thread don't die.. the memory consume will be big... this application will run 24hrs 7days..
Any idea?
Thanks
Bookmarks