Hi borisbn and ajg85 thanks a lot for the answers, they help me to find out the right answer. All I had to do was just like ajg85 said: using a persistent database connection which remains in scope. Also I've found a better way to delete the models QSqlQueryModel and the QTableViews. The QTableViews are deleted when I delete the QTabWidget because the QTabWidget gets the QTableViews as childs by using the inserTab function. For the QSqlQueryModels, I used a QList of QSharedPointers and I don't have to worry for delete them. I include the code for those who had the same problem:
The .h:
class GeneralSearchThread
: public QThread{
Q_OBJECT
public:
explicit GeneralSearchThread
( QObject *parent
= 0 );
~GeneralSearchThread();
void makeQuery
( const QString &query
);
signals:
protected:
void run();
private:
QList< QSharedPointer<QSqlQueryModel> > sqlQueryModelList; // God bless templates.
};
class GeneralSearchThread : public QThread
{
Q_OBJECT
public:
explicit GeneralSearchThread( QObject *parent = 0 );
~GeneralSearchThread();
void makeQuery( const QString &query );
signals:
void modelReadytoRead( QSqlQueryModel *model );
protected:
void run();
private:
QString queryString;
QList< QSharedPointer<QSqlQueryModel> > sqlQueryModelList; // God bless templates.
};
To copy to clipboard, switch view to plain text mode
The .cpp:
GeneralSearchThread
::GeneralSearchThread( QObject *parent
) : QThread( parent
){
}
GeneralSearchThread::~GeneralSearchThread()
{
wait();
}
void GeneralSearchThread
::makeQuery( const QString &query
) {
queryString = query;
start();
}
void GeneralSearchThread::run()
{
if( db.open() )
{
QSharedPointer<QSqlQueryModel> model
= QSharedPointer<QSqlQueryModel>
( new QSqlQueryModel );
sqlQueryModelList.append( model );
model.data()->setQuery( queryString, db );
emit modelReadytoRead( model.data() );
db.close();
}
}
GeneralSearchThread::GeneralSearchThread( QObject *parent ) : QThread( parent )
{
}
GeneralSearchThread::~GeneralSearchThread()
{
wait();
}
void GeneralSearchThread::makeQuery( const QString &query )
{
queryString = query;
start();
}
void GeneralSearchThread::run()
{
QSqlDatabase db = QSqlDatabase::database( "someName" );
if( db.open() )
{
QSharedPointer<QSqlQueryModel> model = QSharedPointer<QSqlQueryModel>( new QSqlQueryModel );
sqlQueryModelList.append( model );
model.data()->setQuery( queryString, db );
emit modelReadytoRead( model.data() );
db.close();
}
}
To copy to clipboard, switch view to plain text mode
To two things. First: there wasn't any problem by calling the db.close() method at the end of the run() method of the QThread.
Second: In my QDialog, in the .h I created a QSqlDatabase db; variable, and in the .cpp in the destructor, I call the db.close() method. I did not call any removeDatabase().
That's all, thanks a lot guys for the answers, Now I run my program and I don't get any nasty message in the console, I just get the beautiful exited with code 0.
Bookmarks