[Qt-interest] Qt::AutoConnection fails

Stefan Bisplinghoff bisplinghoff at hia.rwth-aachen.de
Tue Apr 14 10:42:13 CEST 2009


Hi all!

I have some problems with signals/slots using multiple threads.
There is a "database" with holds my application data and notifies other
components on data changes via signals.
The database and the other components resides in the main thread.

Additionally I acquire data from some external sensors using a separate
thread to avoid blocking of the main application during data aquisition.
The data is inserted in the database afterwards by a direct call of the
database instance.

All receivers of the "dataChanged" signal are connected via
Qt::AutoConnection and work fine for database changes in the main
thread, but emitted signals triggered by database changes in the other
thread are not registered.
Instead all connects have to use direct connections explicitly to get
the signals working.

What I understand is, that connect uses Qt::QueuedConnection if the
signal has been sent from another thread, otherwise Qt::DirectConnection
is used. As shown in the example below, my emit is executed inside the
second thread, but initiated by the database instance, which lives in
the main thread.
Is this the reason, why the AutoConnection gets confused?

Any other suggestions to resolve this issue avoiding the explicit need
of Qt::DirectConnection are also welcome!

Thanks in advance!
Bye,
 Stefan


Example code:

class Database {
public:
	Database() {};
	void modifyDatabase()
	{
		emit dataChanged();
	}
signals:
	void dataChanged();
};

class HardwareAccess : public QThread {
public:
	HardwareAccess( Database* theDatabase ) : m_database(theDatabase);
protected:
	Database* m_database;

	void run()
	{
		forever {
			aquire_some_data();
			m_database->modifyDatabase();
			sleep(1);
		}
	}
};

class SomeComponent {
public:
	SomeComponent( Database* theDatabase ) : m_database(theDatabase)
	{
		// Qt::AutoConnection is used by default!
		connect(m_database,
			SIGNAL(dataChanged()),
			this,
			SLOT(someAction()))
	}
protected slots:
	void someAction()
	{
		do_something_because_database_changed();
	}
};

class MainWindow : public QMainWindow {
protected:
	Database* m_database;
	SomeComponent* m_component;
	HardwareAccess* m_hardwareAccess;
public:
	MainWindow()
	{
		m_database = new Database();
		m_component = new SomeComponent( m_database );
		m_hardwareAccess = new HardwareAccess( m_database );
		m_hardwareAccess->start();
	}
}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5713 bytes
Desc: S/MIME Cryptographic Signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090414/f295501b/attachment.bin 


More information about the Qt-interest-old mailing list