vincecharente

qt5 debug thread debian 10

Mar 30th, 2021
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. -------------------------------------------------------------------------------------------------------------------
  2. test.pro
  3.  
  4. QT -= gui
  5. QT += core
  6. QT += xml
  7.  
  8. #CONFIG += c++11 console
  9. CONFIG += console
  10. CONFIG -= app_bundle
  11.  
  12. # The following define makes your compiler emit warnings if you use
  13. # any Qt feature that has been marked deprecated (the exact warnings
  14. # depend on your compiler). Please consult the documentation of the
  15. # deprecated API in order to know how to port your code away from it.
  16. DEFINES += QT_DEPRECATED_WARNINGS
  17.  
  18. # You can also make your code fail to compile if it uses deprecated APIs.
  19. # In order to do so, uncomment the following line.
  20. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  21. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
  22.  
  23. SOURCES += myclass.cpp
  24. SOURCES += worker.cpp
  25. SOURCES += main.cpp
  26.  
  27. # Default rules for deployment.
  28. # qnx: target.path = /tmp/$${TARGET}/bin
  29. # else: unix:!android: target.path = /opt/$${TARGET}/bin
  30. !isEmpty(target.path): INSTALLS += target
  31.  
  32. HEADERS += \
  33.     myclass.h \
  34.     worker.h
  35.  
  36. -------------------------------------------------------------------------------------------------------------------
  37. main.cpp
  38.  
  39. #include <QCoreApplication>
  40. #include <QDebug>
  41. #include <myclass.h>
  42.  
  43.  
  44. int main(int argc, char *argv[])
  45. {
  46.     QCoreApplication a(argc, argv);    
  47.  
  48.  
  49.     myclass MyClass;
  50.     MyClass.init();
  51.  
  52.  
  53.     int crexec = a.exec();
  54.     return crexec;
  55. }
  56.  
  57.  
  58. -------------------------------------------------------------------------------------------------------------------
  59. myclass.cpp
  60.  
  61. #include "myclass.h"
  62. #include "worker.h"
  63.  
  64. myclass::myclass(QObject *parent) : QObject(parent),
  65.     mWorker(nullptr)
  66. {  
  67.  
  68. }
  69.  
  70. void myclass::init()
  71. {
  72.     QThread* thread = new QThread;
  73.     worker *mWorker = new worker();
  74.     mWorker->moveToThread(thread);
  75.  
  76.     connect(thread, &QThread::started, mWorker, &worker::loop, Qt::DirectConnection);
  77.     connect(mWorker, &worker::finished, thread, &QThread::quit, Qt::DirectConnection);
  78.  
  79.     mWorker->thread()->start();
  80. }
  81.  
  82. void myclass::stop()
  83. {
  84.     mWorker->stop();
  85. }
  86.  
  87. -------------------------------------------------------------------------------------------------------------------
  88. myclass.h
  89.  
  90. #ifndef MYCLASS_H
  91. #define MYCLASS_H
  92.  
  93. #include <QObject>
  94. #include <QThread>
  95. #include "worker.h"
  96.  
  97. class myclass : public QObject
  98. {
  99.     Q_OBJECT
  100. public:
  101.     explicit myclass(QObject *parent = nullptr);
  102.  
  103.     void init();
  104.     void stop();
  105.  
  106. private:
  107.     worker *mWorker;
  108.  
  109. signals:
  110.  
  111. public slots:
  112. };
  113.  
  114. #endif // MYCLASS_H
  115.  
  116. -------------------------------------------------------------------------------------------------------------------
  117. worker.cpp
  118.  
  119. #include "worker.h"
  120. #include <QThread>
  121. #include <QDebug>
  122.  
  123. worker::worker(QObject *parent) : QObject(parent),
  124.     mStop(false)
  125. {
  126.  
  127. }
  128.  
  129. void worker::stop()
  130. {
  131.     mStop = true;
  132. }
  133.  
  134. void worker::loop()
  135. {
  136.     mStop = false;
  137.     int i=0;
  138.  
  139.     while(!mStop) {
  140.         QThread::msleep(500);
  141.  
  142.         qDebug() << "i : " << i;
  143.         i++;
  144.     }
  145.  
  146.     emit finished();
  147. }
  148.  
  149. -------------------------------------------------------------------------------------------------------------------
  150. worker.h
  151.  
  152. #ifndef WORKER_H
  153. #define WORKER_H
  154.  
  155. #include <QObject>
  156.  
  157. class worker : public QObject
  158. {
  159.     Q_OBJECT
  160. public:
  161.     explicit worker(QObject *parent = nullptr);
  162.     void stop();
  163.  
  164. private:
  165.     bool mStop;
  166.  
  167. signals:
  168.     void finished();
  169.  
  170. public slots:
  171.     void loop();
  172. };
  173.  
  174. #endif // WORKER_H
  175.  
Add Comment
Please, Sign In to add comment