[Qt-interest] Qt::AutoConnection fails

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Tue Apr 14 15:21:43 CEST 2009


Thiago Macieira wrote on Tuesday, April 14, 2009 2:37 PM:

> Em Terça-feira 14 Abril 2009, às 11:25:48, Oliver.Knoll at comit.ch
> escreveu: 
>> First off, it is important to realise that Qt::AutoConnect decides at
>> *connection time* whether Qt::QueuedConnection or
>> Qt::DirectConnection has to be used. It does this by comparing the
>> threads (http://doc.trolltech.com/4.5/qobject.html#thread) to which
>> the QObject based object is associated with, at the point where
>> connect() is called. If both objects reside in the same thread, then
>> Qt::DirectConnection will be used, otherwise Qt::QueuedConnection.
> 
> That's actually incorrect.
> 
> The decision on whether to queue or to deliver directly in the case
> of an AutoConnection is done at emission time, not at connection
> time. If either the sender or receiver belong to another thread, the
> emission is queued.   

Oh okay, I should have checked the Qt docs. But I remember that there was a warning somewhere in the Qt 4.0.x docs (or any other Qt related documentation, because honestly I don't find the reference anymore ;), saying that one should pay attention as to *when exactly* call connect(), because it decides *at that time* what kind of connection should be used.

Specifically I have the following example in mind (which I think was given in that documentation). Given the class declaration:

  class MyThread : public QThread {
    Q_OBJECT
  public:
    MyThread();
  signals:
    void foo();
  protected:
    virtual void run();
  private:
    MyObject m_myObject;
  };

Connecting the signal/slots in the c'tor of that class behaves differently (DIRECT connection):

  MyThread::MyThread() {
    // m_myObject and 'this' belong to same thread -> DIRECT connection
    connect(this, SIGNAL(foo()),
                &m_myObject, SLOT(bar()));
    ...
 }

  void MyThread::run() {...}

than when connecting in the run() method (QUEUED connection):

  MyThread::MyThread() {...}

  void MyThread::run() {
    // m_myObject and 'this' belong now to DIFFERENT threads -> QUEUED connection
    connect(this, SIGNAL(foo()),
                &m_myObject, SLOT(bar()));
    ...
  }

Anyone else remembers this kind of example?

As I said, I can't remember /where/ I read this, but I seem to remember /that/ I read it :) - and it was indeed in some "Qt documentation" (tutorial, QQ, ...). And the lesson I learned from that day on: never rely on the "AutoConnect" feature!

But as you said, the above apparently is not valid (anymore?), for sure not with Qt 4.5 or so, so please forget immediatelly what I wrote (especially to the OP). But I probably know now what the OP's problem could be (no event queue in the receiving thread?).


Cheers, Oliver
-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22



More information about the Qt-interest-old mailing list