Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. --- main.cpp ---
  2. #include <QCoreApplication>
  3. #include "a.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     QCoreApplication app(argc, argv);
  8.  
  9.     QElapsedTimer timer;
  10.     timer.start();
  11.     A a;
  12.     a.process();
  13.     qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO << "needed" << timer.elapsed();
  14.  
  15.     return app.exec();
  16. }
  17.  
  18. --- a.cpp ---
  19. #include "a.h"
  20. #include "b.h"
  21.  
  22. A::A(QObject *parent) :
  23.     QObject(parent),
  24.     m_b(new B(this)){
  25.     connect(m_b, &B::finished, this, &A::loopFinished);
  26. }
  27.  
  28. A::~A() {
  29.     if (m_b) {
  30.         delete m_b;
  31.         m_b = nullptr;
  32.     }
  33. }
  34.  
  35. void A::process() {
  36.     qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
  37.  
  38.     //
  39.     // start event-loop and wait for signal finished()
  40.     //
  41.     QEventLoop loop;
  42.     connect(m_b, &B::finished, &loop, &QEventLoop::quit);
  43.  
  44.     //
  45.     // use timeout
  46.     //
  47.     QTimer timer;
  48.     timer.setSingleShot(true);
  49.     connect(&timer, &QTimer::timeout, this, &A::timeout);
  50.     connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
  51.     timer.start(2000);
  52.  
  53.     //
  54.     // exec loop
  55.     //
  56.     loop.exec();
  57.     qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
  58.  
  59.     //
  60.     // is timer active?
  61.     //
  62.     if (timer.isActive()) {
  63.         qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO << "ok";
  64.     } else {
  65.         qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO << "timeout";
  66.     }
  67. }
  68.  
  69. void A::loopFinished() {
  70.     qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
  71. }
  72.  
  73. void A::timeout() {
  74.     qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
  75.  
  76.     QTimer *timer = static_cast<QTimer *>(sender());
  77.     if (timer) {
  78.         qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO << "timeout";
  79.     }
  80. }
  81.  
  82. --- a.h ---
  83. #pragma once
  84.  
  85. #include <QtCore>
  86.  
  87. class B;
  88.  
  89. class A : public QObject
  90. {
  91.     Q_OBJECT
  92. public:
  93.     explicit            A(QObject *parent = nullptr);
  94.                         ~A();
  95.     void                process();
  96.  
  97. private:
  98.     B                   *m_b = nullptr;
  99.  
  100. private slots:
  101.     void                loopFinished();
  102.     void                timeout();
  103. };
  104.  
  105. --- b.cpp ---
  106. #include "b.h"
  107.  
  108. B::B(QObject *parent) :
  109.     QObject(parent) {
  110.     QTimer::singleShot(5000, this, &B::test);
  111. }
  112.  
  113. B::~B() {
  114.  
  115. }
  116.  
  117. void B::test() {
  118.     qDebug() << QDateTime::currentMSecsSinceEpoch() << Q_FUNC_INFO;
  119.     emit finished();
  120. }
  121.  
  122. --- b.h ---
  123. #pragma once
  124.  
  125. #include <QtCore>
  126.  
  127. class B : public QObject
  128. {
  129.     Q_OBJECT
  130. public:
  131.     explicit            B(QObject *parent = nullptr);
  132.                         ~B();
  133. signals:
  134.     void                finished();
  135.  
  136. private slots:
  137.     void                test();
  138. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement