Guest User

Untitled

a guest
Jan 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. MyClass::MyClass(...)
  2. {
  3. OtherClass::getInstance()->setCallback(this);
  4. connect(this, SIGNAL(mySignalToMainThread()), this, SLOT(doThisOnMainThread()));
  5. }
  6.  
  7. // Public slot, called by OtherClass on its own thread.
  8. void MyClass::someCallback()
  9. {
  10. emit mySignalToMainThread();
  11. }
  12.  
  13. void MyClass::doThisOnMainThread()
  14. {
  15. // AHHH! I am still on callers thread.
  16. }
  17.  
  18. void MyClass::someCallback()
  19. {
  20. Q_ASSERT(QThread::currentThread() != this->thread());
  21. emit mySignalToMainThread();
  22. }
  23.  
  24. void MyClass::doThisOnMainThread()
  25. {
  26. Q_ASSERT(QThread::currentThread() == this->thread());
  27. }
  28.  
  29. class MyClass : public QObject {
  30. Q_OBJECT
  31.  
  32. public:
  33. explicit MyClass(QObject *parent = 0);
  34.  
  35. signals:
  36. void mySignalToMainThread();
  37.  
  38. public slots:
  39. void someCallback();
  40. void doThisOnMainThread();
  41. };
  42.  
  43. MyClass::MyClass(QObject *parent) :
  44. QObject(parent) {
  45. std::cout << Q_FUNC_INFO << QThread::currentThreadId() << std::endl;
  46. OtherClass::getInstance().setCallback(this);
  47. connect(this, SIGNAL(mySignalToMainThread()), SLOT(doThisOnMainThread()));
  48. }
  49.  
  50. void MyClass::someCallback() {
  51. std::cout << Q_FUNC_INFO << QThread::currentThreadId() << std::endl;
  52. emit mySignalToMainThread();
  53. }
  54.  
  55. void MyClass::doThisOnMainThread() {
  56. std::cout << Q_FUNC_INFO << QThread::currentThreadId() << std::endl;
  57. }
  58.  
  59. class OtherClass : public QObject {
  60. Q_OBJECT
  61.  
  62. public:
  63. static OtherClass& getInstance();
  64. void setCallback(MyClass *cb);
  65.  
  66. public slots:
  67. void doCallback();
  68.  
  69. private:
  70. explicit OtherClass(QObject *parent = 0);
  71.  
  72. MyClass *cb_;
  73. };
  74.  
  75. OtherClass::OtherClass(QObject *parent) : QObject(parent) {
  76. std::cout << Q_FUNC_INFO << QThread::currentThreadId() << std::endl;
  77. }
  78.  
  79. OtherClass& OtherClass::getInstance() {
  80. std::cout << Q_FUNC_INFO << QThread::currentThreadId() << std::endl;
  81. static OtherClass singleton;
  82. return singleton;
  83. }
  84.  
  85. void OtherClass::doCallback() {
  86. std::cout << Q_FUNC_INFO << QThread::currentThreadId() << std::endl;
  87. cb_->someCallback();
  88. }
  89.  
  90. void OtherClass::setCallback(MyClass *cb) {
  91. std::cout << Q_FUNC_INFO << QThread::currentThreadId() << std::endl;
  92. cb_ = cb;
  93. }
  94.  
  95. int main (int argc, char **argv) {
  96. QApplication app(argc, argv);
  97.  
  98. MyClass c;
  99.  
  100. QThread other;
  101. OtherClass::getInstance().moveToThread(&other);
  102. other.connect(&other, SIGNAL(started()),
  103. &OtherClass::getInstance(), SLOT(doCallback()));
  104. other.start();
  105.  
  106. QMainWindow w;
  107. w.show();
  108.  
  109. return app.exec();
  110. }
  111.  
  112. __thiscall MyClass::MyClass(class QObject *)00001108
  113. class OtherClass &__cdecl OtherClass::getInstance(void)00001108
  114. __thiscall OtherClass::OtherClass(class QObject *)00001108
  115. void __thiscall OtherClass::setCallback(class MyClass *)00001108
  116. class OtherClass &__cdecl OtherClass::getInstance(void)00001108
  117. class OtherClass &__cdecl OtherClass::getInstance(void)00001108
  118. void __thiscall OtherClass::doCallback(void)000015AC
  119. void __thiscall MyClass::someCallback(void)000015AC
  120. void __thiscall MyClass::doThisOnMainThread(void)00001108
Add Comment
Please, Sign In to add comment