Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- main.cpp ---
- #include <QCoreApplication>
- #include "a.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication app(argc, argv);
- QElapsedTimer timer;
- timer.start();
- A a;
- a.process();
- qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO << "needed" << timer.elapsed();
- return app.exec();
- }
- --- a.cpp ---
- #include "a.h"
- #include "b.h"
- A::A(QObject *parent) :
- QObject(parent),
- m_b(new B(this)){
- connect(m_b, &B::finished, this, &A::loopFinished);
- }
- A::~A() {
- if (m_b) {
- delete m_b;
- m_b = nullptr;
- }
- }
- void A::process() {
- qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
- //
- // start event-loop and wait for signal finished()
- //
- QEventLoop loop;
- connect(m_b, &B::finished, &loop, &QEventLoop::quit);
- //
- // use timeout
- //
- QTimer timer;
- timer.setSingleShot(true);
- connect(&timer, &QTimer::timeout, this, &A::timeout);
- connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
- timer.start(2000);
- //
- // exec loop
- //
- loop.exec();
- qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
- //
- // is timer active?
- //
- if (timer.isActive()) {
- qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO << "ok";
- } else {
- qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO << "timeout";
- }
- }
- void A::loopFinished() {
- qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
- }
- void A::timeout() {
- qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
- QTimer *timer = static_cast<QTimer *>(sender());
- if (timer) {
- qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO << "timeout";
- }
- }
- --- a.h ---
- #pragma once
- #include <QtCore>
- class B;
- class A : public QObject
- {
- Q_OBJECT
- public:
- explicit A(QObject *parent = nullptr);
- ~A();
- void process();
- private:
- B *m_b = nullptr;
- private slots:
- void loopFinished();
- void timeout();
- };
- --- b.cpp ---
- #include "b.h"
- B::B(QObject *parent) :
- QObject(parent) {
- QTimer::singleShot(5000, this, &B::test);
- }
- B::~B() {
- }
- void B::test() {
- qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
- emit finished();
- }
- --- b.h ---
- #pragma once
- #include <QtCore>
- class B : public QObject
- {
- Q_OBJECT
- public:
- explicit B(QObject *parent = nullptr);
- ~B();
- signals:
- void finished();
- private slots:
- void test();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement