0

私は自分のプログラムでこの関数を使用しました:

void delay(QState * state1, int millisecond, QAbstractState * state2) 
{
   auto timer = new QTimer(state1);
   timer->setSingleShot(true);
   timer->setInterval(millisecond);

   QObject::connect(state1, &QState::entered, timer, static_cast<void (QTimer::*)()>(&QTimer::start));
   QObject::connect(state1, &QState::exited,  timer, &QTimer::stop);

   state1 -> addTransition(timer, SIGNAL(timeout()), state2);
}

例からコピーして貼り付けましたが、コードのこの部分がわかりませんでした:

QObject::connect(state1,..., static_cast<void (QTimer::*)()>(&QTimer::start));

誰でもこのコードが何であるかを説明できますか? プログラムでどのように機能しますか?

PS。私はこれでそのコードを変更しようとしましたが、うまくいきませんでした:

QTimer *timer = new QTimer(state1);
.
.  //same code as before
.
QObject::connect(stato1,&QState::entered,timer,[&] {timer->start();} );
QObject::connect(stato1,&QState::exited, timer,[&] {timer->stop(); } );

stato1 -> addTransition(timer,SIGNAL(timeout()),stato2);
4

1 に答える 1

3

2 つのQTimer::startスロットがあり、1 つはパラメーターなし、もう 1 つはint msecパラメーター付きです。新しい接続構文で正しいものに接続するには、スロット タイプを . で指定する必要がありますstatic_cast

したがって、この行では:

QObject::connect(state1, &QState::entered, timer, static_cast<void (QTimer::*)()>(&QTimer::start));

QTimer::start引数を取らないスロットに接続します。

intパラメータ付きのシグナルがあり、QTimer::start(int msec)スロットに接続したい場合は、次のようにします。

connect(this, &MyClass::mySignal, timer, static_cast<void (QTimer::*)(int)>(&QTimer::start));

新しい接続構文でオーバーロードされたシグナル/スロットを使用する方法の詳細については、こちらを参照してください。

qOverload醜いの必要性を取り除くためにも使用できますstatic_cast

ラムダ式を使用するスニペットでは、timer参照によってキャプチャします。代わりに値でキャプチャする必要があります。

QObject::connect(stato1, &QState::entered, timer, [=]{timer->start();});
于 2016-06-09T11:54:49.223 に答える