Netheril
6th April 2010, 02:16
Hi, I'm making multiples queries to a database by just pressing a QPushButton and reading the query from a QLineEdit. I need to show that queries in a QTabWidget that has multiples QTableViews each one in a different tab. I created a QThread, an in the run() a send a signal with one dynamic model to the QDialog where I have the QTabWidget. Here's the code:
class GeneralSearchThread : public QThread
{
Q_OBJECT
public:
explicit GeneralSearchThread( QObject *parent = 0 );
~GeneralSearchThread();
void makeQuery( const QString &query );
signals:
void modelReadytoRead( QSqlQueryModel *model ); //this signal will be connected to the QDialog.
public slots:
protected:
void run();
private:
QString nameUser;
QString passUser;
QString queryString;
QList< QSqlQueryModel *> listaModels;
};
GeneralSearchThread::GeneralSearchThread( QObject *parent ) : QThread( parent )
{
}
GeneralSearchThread::~GeneralSearchThread()
{
wait();
qDeleteAll(listaModels); /// I get the problem here!!!
listaModels.clear(); /// I get the problem here!!!
}
void GeneralSearchThread::makeQuery( const QString &query )
{
queryString = query;
start();
}
void GeneralSearchThread::run()
{
QSqlDatabase db = QSqlDatabase::database( "someName" );
if( db.open() )
{
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery( queryString, db );
listaModels.append( model );
emit modelReadytoRead( model );
db.close();
}
}
The question here, is that I'm creating the QSqlQueryModels dynamically and trying to store them in a QList (which I call listaModels) and delete it in the destructor of the QThread. But I get a problem of Segmentation fault . But when I put away the lines inside the destructor I get no problems, but I'm afraid of a memory leak. Does anyone know how to solve this problem?, the QSqlQueryModels get deleted when the driver is closed and I'm re-deleting them?. Thanks a lot in advanced.
Extra Info: Driver: QMYSQL3, OS: Ubuntu 9.04, gcc version 4.4.1, Qt version 4.6.
class GeneralSearchThread : public QThread
{
Q_OBJECT
public:
explicit GeneralSearchThread( QObject *parent = 0 );
~GeneralSearchThread();
void makeQuery( const QString &query );
signals:
void modelReadytoRead( QSqlQueryModel *model ); //this signal will be connected to the QDialog.
public slots:
protected:
void run();
private:
QString nameUser;
QString passUser;
QString queryString;
QList< QSqlQueryModel *> listaModels;
};
GeneralSearchThread::GeneralSearchThread( QObject *parent ) : QThread( parent )
{
}
GeneralSearchThread::~GeneralSearchThread()
{
wait();
qDeleteAll(listaModels); /// I get the problem here!!!
listaModels.clear(); /// I get the problem here!!!
}
void GeneralSearchThread::makeQuery( const QString &query )
{
queryString = query;
start();
}
void GeneralSearchThread::run()
{
QSqlDatabase db = QSqlDatabase::database( "someName" );
if( db.open() )
{
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery( queryString, db );
listaModels.append( model );
emit modelReadytoRead( model );
db.close();
}
}
The question here, is that I'm creating the QSqlQueryModels dynamically and trying to store them in a QList (which I call listaModels) and delete it in the destructor of the QThread. But I get a problem of Segmentation fault . But when I put away the lines inside the destructor I get no problems, but I'm afraid of a memory leak. Does anyone know how to solve this problem?, the QSqlQueryModels get deleted when the driver is closed and I'm re-deleting them?. Thanks a lot in advanced.
Extra Info: Driver: QMYSQL3, OS: Ubuntu 9.04, gcc version 4.4.1, Qt version 4.6.