2017-02-10 16 views
2

私はQObjectの信号にラムダを接続しています:ラムダ接続時にQt :: ConnectionTypeをQObject :: connectに渡すには?

QObject::connect(handle, &BatchHandle::progressMax, [this](const ProcessHandle* const self, const int value) { 
     this->maxProgress(value); 
    }); 

上記のコードは問題なくコンパイルされます。

handleオブジェクトが最終的に別のスレッドに移動するため、Qt::QueuedConnectionが絶対に必要です。

私は自分のコードにこれを追加しました:私はそれが正しく値の型を特定することを確認するために明示的なキャストを追加する方法

QObject::connect(handle, &BatchHandle::finished, [this](const ProcessHandle* const self) { 
     this->processIsRunning(false); 
    }, (Qt::ConnectionType)Qt::QueuedConnection); 

お知らせ。結果:

1>src\TechAdminServices\database\techCore\processes\import\ImportManagerDialog.cpp(191): error C2664: 'QMetaObject::Connection QObject::connect<void(__cdecl taservices::ProcessHandle::*)(const taservices::ProcessHandle *),Qt::ConnectionType>(const taservices::ProcessHandle *,Func1,const QObject *,Func2,Qt::ConnectionType)' : cannot convert parameter 3 from 'taservices::`anonymous-namespace'::<lambda58>' to 'const QObject *' 
1>   with 
1>   [ 
1>    Func1=void (__cdecl taservices::ProcessHandle::*)(const taservices::ProcessHandle *), 
1>    Func2=Qt::ConnectionType 
1>   ] 
1>   No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 

ラムダ接続時のキューイング方法は?

+0

ターゲットコンテキストの要件によって、直感的ではありませんが、明らかにそうではないため、これは二重であると確信していました。 –

答えて

6

私はキューに入れられた接続があるため、対象オブジェクトコンテキストなしで動作することはできません

QObject::connect(
    handle, 
    &BatchHandle::progressMax, 
    target_context, /* Target context parameter. */ 
    [this](const ProcessHandle* const self, const int value) 
    { 
    this->maxProgress(value); 
    }, 
    Qt::QueuedConnection); 
0

...あなたは、ラムダが呼び出されるべきでコンテキストを指定することができますQObject::connect overloadを使用する必要があると思いますスロットコールが挿入されるキューを選択するのはこのコンテキストです。 To be more obtuselyの場合、ファンクタをラップするQMetaCallEventがコンテキストオブジェクトthread()のイベントキューにポストされます。

関連する問題