Advertisement
ebak32

Qt interthread signals

Apr 21st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. // ------- worker.h ------------
  2. #ifndef WORKER_H
  3. #define WORKER_H
  4.  
  5. #include <QObject>
  6. #include "CL/clmain.h"
  7.  
  8. class Worker : public QObject
  9. {
  10.     Q_OBJECT
  11. public:
  12.     explicit Worker(QObject *parent = nullptr);
  13.  
  14. protected:
  15.     CLMain clMain;
  16.  
  17. signals:
  18.     void error(const std::string msg);
  19.  
  20. public slots:
  21.     void run();
  22. };
  23.  
  24. #endif // WORKER_H
  25.  
  26.  
  27. // ----- worker.cpp -----------
  28. #include "worker.h"
  29. #include <iostream>
  30.  
  31. Worker::Worker(QObject *parent) : QObject(parent) {}
  32.  
  33. void Worker::run() {
  34.     std::cout << "run()\n" << std::flush;
  35.     try {
  36.         clMain.getDevice();
  37.         clMain.loadProgram();
  38.         std::cout << "emit OK\n" << std::flush;
  39.         emit error(std::string("OK"));
  40.     } catch (std::string msg) {
  41.         std::cout << "emit " << msg << "\n" << std::flush;
  42.         emit error(msg);
  43.     }
  44. }
  45.  
  46. // ------------ mywidget.h ----------------
  47. #ifndef MYWIDGET_H
  48. #define MYWIDGET_H
  49.  
  50. #include <QWidget>
  51. #include <QTimer>
  52. #include <QImage>
  53. #include <QThread>
  54. #include <cmath>
  55. #include "worker.h"
  56.  
  57. class MyWidget : public QWidget
  58. {
  59.     Q_OBJECT
  60.  
  61. public:
  62.     explicit MyWidget(QWidget *parent = nullptr);
  63.     virtual ~MyWidget();
  64.     virtual void paintEvent(QPaintEvent*);
  65.  
  66. signals:
  67.     void start();
  68.  
  69. public slots:
  70.     void clError(std::string msg);
  71.  
  72. private:
  73.     uint32_t getOffs(float x, float y) {
  74.         return round(x) + round(y) * width();
  75.     }
  76.  
  77.     float ang = 0;
  78.     Worker worker;
  79.     QThread workerThread;
  80.     QTimer * timer;
  81.     QImage * img;
  82.     uint32_t * buffer = nullptr;
  83.  
  84.     void updateImage();
  85. };
  86.  
  87. #endif // MYWIDGET_H
  88.  
  89.  
  90. // ------------- mywidget.cpp -------------
  91. #include "mywidget.h"
  92. #include <QPainter>
  93. #include <QMessageBox>
  94. #include <iostream>
  95.  
  96. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
  97. {
  98.     std::cout << "ctor\n" << std::flush;
  99.     timer = new QTimer(this);   // destruction is made by the Qt framework
  100.     connect(timer, SIGNAL(timeout()), this, SLOT(repaint()));
  101.     timer->start(50);
  102.     img = nullptr;
  103.     worker.moveToThread(&workerThread);
  104.     connect(&worker, SIGNAL(error()), this, SLOT(clError()));
  105.     // connect(&worker, &Worker::error, this, &MyWidget::clError);
  106.     connect(this, SIGNAL(start()), &worker, SLOT(run()));
  107.     workerThread.start();
  108.     emit start();
  109. }
  110.  
  111. MyWidget::~MyWidget() {
  112.     workerThread.quit();
  113.     workerThread.wait();
  114.     delete img;
  115.     delete[] buffer;
  116. }
  117.  
  118.  
  119. void MyWidget::paintEvent(QPaintEvent *e) {
  120.     updateImage();
  121.     QPainter p(this);
  122.     p.drawImage(QPointF(0, 0), *img);
  123. }
  124.  
  125.  
  126. void MyWidget::updateImage() {
  127.     if (img == nullptr) {
  128.         int w = width(); int h = height();
  129.         buffer = new uint32_t[w * h];
  130.         for(int i = 0; i < w * h; i++) {
  131.             buffer[i] = 0xff000000;
  132.         }
  133.         img = new QImage((unsigned char *)buffer, w, h, QImage::Format_RGB32);
  134.     }
  135.     uint32_t * data = (uint32_t*)(uchar *)img->bits();
  136.     float cx = 0.5f * width();
  137.     float cy = 0.5f * height();
  138.     float pAng = ang - 0.25 * M_PI;
  139.     data[getOffs(cx + 100 * cos(ang), cy + 100 * sin(ang))] = 0xff8080;
  140.     data[getOffs(cx + 100 * cos(pAng), cy + 100 * sin(pAng))] = 0x0;
  141.     ang += 0.01 * M_PI;
  142.     if (ang > 2 * M_PI) {
  143.         ang -= 2 * M_PI;
  144.     }
  145.  }
  146.  
  147. void MyWidget::clError(const std::string msg) {
  148.     std::cout << "clError('" << msg << "')\n" << std::flush;
  149.     QMessageBox::critical(this, QString("OpenCL error"), QString(msg.c_str()));
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement