ich hab hier eine Klasse, die Daten aus einer Datenbank laedt. Dazu rufe ich den Slot "getData" ueber Queued connections auf. So, an sich funktioniert alles, aber eben nicht immer. Ich hab ein paar Debuggingausgaben eingebaut, die im allgemeinen wie folgt aussehen:
Code: Alles auswählen
get: "splashes" 1
get: "splashes" 2
run: "splashes" 1
fin: "splashes" 1
run: "splashes" 2
get: "splashes" 3
fin: "splashes" 2
run: "splashes" 3
get: "img_images" 1
run: "img_images" 1
fin: "splashes" 3
fin: "img_images" 1Header:
Code: Alles auswählen
class BinaryLoader : public QThread
{
Q_OBJECT
public:
BinaryLoader ( );
~BinaryLoader ( );
public slots:
void getData ( const QString&, const int& );
signals:
void dataLoaded ( const int&, const QByteArray& );
protected:
void run();
QString tableName;
int id;
private:
QMutex mutex;
};Code: Alles auswählen
BinaryLoader::BinaryLoader ( ) : QThread ( )
{
}
BinaryLoader::~BinaryLoader()
{
}
void BinaryLoader::getData ( const QString &tableName, const int &id )
{
qDebug() << "get: " << tableName << " " << id;
mutex.lock();
this->tableName = tableName;
this->id = id;
mutex.unlock();
start();
}
void BinaryLoader::run()
{
mutex.lock();
qDebug() << "run: " << tableName << " " << id;
QString _tableName = this->tableName;
int _id = this->id;
qsrand ( QTime::currentTime().msec() );
QString connectionName = QString ( "thread_%1_%2_%3" ).arg ( qrand() ).arg ( _tableName ).arg ( _id );
{
QSqlDatabase db = QSqlDatabase::addDatabase ( "QSQLITE", connectionName );
db.setDatabaseName ( QSqlDatabase::database().databaseName () );
db.open();
bool good = true;
QSqlQuery query ( QSqlDatabase::database ( connectionName ) );
query.prepare ( QString ( "SELECT data FROM %1 WHERE id = :id" ).arg ( tableName ) );
query.bindValue ( ":id", id );
if ( !query.exec() ) good = false;
if ( !query.next() ) good = false;
if ( good )
{
emit dataLoaded ( id, query.value ( 0 ).toByteArray ( ) );
}
query.clear();
}
QSqlDatabase::removeDatabase ( connectionName );
qDebug() << "fin: " << tableName << " " << id;
mutex.unlock();
}Code: Alles auswählen
connect ( this, SIGNAL ( loadImage ( QString, int ) ), loader, SLOT ( getData ( QString, int ) ), Qt::QueuedConnection );
connect ( loader, SIGNAL ( dataLoaded ( int, QByteArray ) ), this, SLOT ( display ( int, QByteArray ) ), Qt::QueuedConnection );Code: Alles auswählen
emit loadImage ( QString ( "splashes" ), 1 );
emit loadImage ( QString ( "splashes" ), 2 );
emit loadImage ( QString ( "splashes" ), 3 );