代码之家  ›  专栏  ›  技术社区  ›  Anon

让程序在Qt中等待信号的最简单方法是什么?

  •  1
  • Anon  · 技术社区  · 8 年前

    所以我知道,在Qt中,你可以在信号发生后将你想要的一切放入一个槽中,但在以后编辑代码时;这可能需要大量重组,并可能使事情复杂化。

    有没有更简单的方法来保持程序直到Qt中发出信号,例如:

    downloadImage();
    Qt::waitForSignal(downloadImageFinished());
    editImage();
    

    而且为什么这样的东西不起作用:

    // bool isFinished();
    downloadImage(); // When done, isFinished() returns true;
    while (!isFinished()) {}
    editImage();
    

    ? 谢谢

    1 回复  |  直到 8 年前
        1
  •  10
  •   drescherjm    8 年前

    基本上,你应该这样做:

        QEventLoop loop;
        connect(this, &SomeObject::someSignal, &loop, &QEventLoop::quit);
        // here you can send your own message to signal the start of wait, 
        // start a thread, for example.
        loop.exec(); //exec will delay execution until the signal has arrived