///////////////////////////////////////////////////////// MAIN.CPP
PostgresDriver* driver = new PostgresDriver("localhost" , 5432 , "database" , "user" , "user" , "main");
QListIterator<QSqlRecord> i( driver->executeQuery( "SELECT * FROM WHATEVER" ) );
while( i.hasNext() ){
}
driver->deleteLater();
///////////////////////////////////////////////////////// MAIN.CPP
PostgresDriver* driver = new PostgresDriver("localhost" , 5432 , "database" , "user" , "user" , "main");
QListIterator<QSqlRecord> i( driver->executeQuery( "SELECT * FROM WHATEVER" ) );
while( i.hasNext() ){
}
driver->deleteLater();
To copy to clipboard, switch view to plain text mode
Works perfectly.
Now:
///////////////////////////////////////////////////////// AGENT.H
{
Q_OBJECT
public:
Agent
(QString class_name
= "Agent");
~Agent();
// GETTERS
unsigned int getId();
slots:
virtual void start();
virtual void end(){this->deleteLater();}
private:
// INCREMENTAL FOR IDS
static unsigned int counter;
unsigned int id;
};
///////////////////////////////////////////////////////// AGENT.CPP
#include "Agent.h"
unsigned int Agent::counter = 0;
{
this->id = ++Agent::counter;
this->class_name = class_name;
this
->setObjectName
( QString("%1%2").
arg( this
->getClass
() ).
arg( this
->getId
() ) );
}
Agent::~Agent() {
this->thread()->deleteLater();
}
unsigned int Agent::getId(){
return this->id;
}
return this->class_name;
}
void Agent::start(){
PostgresDriver* db = new PostgresDriver("localhost" , 5432 , "database" , "user" , "user" , this->objectName() ); //CRASHES
}
///////////////////////////////////////////////////////// MAIN.CPP
Agent* agent = new Agent("Car");
agent->moveToThread( thread );
agent->connect( thread, SIGNAL( started() ), agent, SLOT( start() ) );
thread->start();
///////////////////////////////////////////////////////// AGENT.H
class Agent : public QObject
{
Q_OBJECT
public:
Agent(QString class_name = "Agent");
~Agent();
// GETTERS
unsigned int getId();
QString getClass();
slots:
virtual void start();
virtual void end(){this->deleteLater();}
private:
// INCREMENTAL FOR IDS
static unsigned int counter;
unsigned int id;
QString class_name;
};
///////////////////////////////////////////////////////// AGENT.CPP
#include "Agent.h"
unsigned int Agent::counter = 0;
Agent::Agent(QString class_name) : QObject()
{
this->id = ++Agent::counter;
this->class_name = class_name;
this->setObjectName( QString("%1%2").arg( this->getClass() ).arg( this->getId() ) );
}
Agent::~Agent() {
this->thread()->deleteLater();
}
unsigned int Agent::getId(){
return this->id;
}
QString Agent::getClass(){
return this->class_name;
}
void Agent::start(){
PostgresDriver* db = new PostgresDriver("localhost" , 5432 , "database" , "user" , "user" , this->objectName() ); //CRASHES
}
///////////////////////////////////////////////////////// MAIN.CPP
Agent* agent = new Agent("Car");
QThread *thread = new QThread( );
agent->moveToThread( thread );
agent->connect( thread, SIGNAL( started() ), agent, SLOT( start() ) );
thread->start();
To copy to clipboard, switch view to plain text mode
Crashes when creating a PostgresDriver inside the Agent.
Bookmarks