Advertisement
gadLinux

C++ Qt Emit Bug

Oct 4th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. --------  in mqmessagemanager.cpp --------------------------
  2.  
  3. // This is the static wrapper for the class
  4. // The class comes from a gpointer (void*) data
  5. // And it's converted to a class pointer.
  6. // Then we just simply call a method.
  7.  
  8. // Since the call back comes from glib is a plain pthread thread.
  9.  
  10. static gboolean callbackWrapper(SG64ZeroMQ* mq, ThriftStruct* message, gpointer data, GError **error) {
  11.     qDebug("Message received");
  12.     MQMessageManager* self = static_cast<MQMessageManager*>(data);
  13.  
  14.     fflush(stdout);
  15.     return self->processMessage(mq, message, error);
  16. }
  17.  
  18.  
  19. // Other method of the class
  20. bool MQMessageManager::processMessage(SG64ZeroMQ* mq, ThriftStruct* message, GError **error)
  21. {
  22.     mqPayload *payload=NULL;
  23.     mqCardEvent *cardEvent = NULL;
  24.     mqMessage *msg=NULL;
  25.     QString str;
  26.     g_return_val_if_fail (THRIFT_IS_STRUCT (message), FALSE);
  27.  
  28. //    if(QThread::self()==NULL){
  29. //        qDebug("Not in thread");
  30. //    }
  31.  
  32.     if(IS_MQ_MESSAGE(message))
  33.       msg=MQ_MESSAGE(message);
  34.     switch(msg->type){
  35.       case MQ_MESSAGE_TYPE_COMMAND:
  36.         puts("Received a command");
  37.         break;
  38.       case MQ_MESSAGE_TYPE_GPS_EVENT:
  39.         puts("Received a gps event");
  40.         break;
  41.       case MQ_MESSAGE_TYPE_CARD_EVENT:
  42.         if(IS_MQ_CARD_EVENT(msg->payload->card_event)){
  43.             cardEvent = msg->payload->card_event;
  44.             if(cardEvent!=NULL && cardEvent->atr!=NULL){
  45.                 QByteArray array((char *)cardEvent->atr->data, (int) cardEvent->atr->len);
  46.                 QString atr = QString(array.toHex());
  47.                 qDebug("ATR found of size %d, %s", cardEvent->atr->len, atr.toLatin1().data());
  48. //                emit valid(atr);
  49.                 emit cardValidationSuccess(); // It breaks here!!!!!!!!!!!!!
  50.                 qDebug("Emitted valid signal");
  51.                 QThread *thread = QThread::currentThread();
  52.                 qDebug("Emitted valid signal %x", thread);
  53.  
  54. //                thread->
  55. //                QMetaObject::invokeMethod(this, "valid", Qt::QueuedConnection,
  56. //                                          Q_ARG(QString, atr));
  57.             }
  58.         }
  59.         break;
  60.       default:
  61.         printf("Message of type %i cannot be managed\n", msg->type);
  62.         break;
  63.     }
  64.     return TRUE;
  65. }
  66.  
  67.  
  68.  
  69. --------  in mqmessagemanager.cpp --------------------------
  70.  
  71.  
  72.  
  73.  
  74. --------  in mainwindow.cpp --------------------------
  75.  
  76.  
  77. // I just connect few signals
  78. // Can even move (by comments) the class to a new thread
  79. // but it makes no difference on the bug. It breaks the same
  80. void MainWindow::startManagement()
  81. {
  82.     ui->textLabel->show();
  83.     qDebug("Starting management");
  84.  
  85. //    QThread *rfidThread = new QThread(this);
  86. //    QObject::connect(rfidThread, SIGNAL(started()), nfcReader, SLOT(startrfid()));
  87.     if(!QObject::connect(mqMessageManager, SIGNAL(cardValidationSuccess()), this, SLOT(displayValid()),Qt::QueuedConnection)){
  88.         qDebug("Cannot connect signal");
  89.     }
  90.  
  91.     QObject::connect(mqMessageManager, SIGNAL(valid(QString)), this, SLOT(displayValid(QString)), Qt::BlockingQueuedConnection);
  92. //    QObject::connect(mqMessageManager, SIGNAL(invalid(QString)), this, SLOT(displayInvalid(QString)), Qt::BlockingQueuedConnection);
  93. //    QObject::connect(mqMessageManager, SIGNAL(concession(QString)), this, SLOT(displayConcession(QString)), Qt::BlockingQueuedConnection);
  94. //    mqMessageManager->moveToThread(rfidThread);
  95. //    rfidThread->start( );
  96.     QTimer::singleShot(0, mqMessageManager, SIGNAL(startMQMessenging()));
  97.  
  98. }
  99.  
  100.  
  101. void MainWindow::displayValid()
  102. {
  103.  
  104.  
  105.     green_light();
  106.     QPixmap pixmap(qApp->applicationDirPath()+"/images/valid.png");
  107.     ui->imageLabel->setPixmap(pixmap);
  108.     ui->imageLabel->setMask(pixmap.mask());
  109.     ui->textLabel->setText(message);
  110.     ui->textLabel->show();
  111.     play_good();
  112.  
  113.     timer.start(SCREENTIME);
  114. }
  115.  
  116.  
  117.  
  118. --------  in mainwindow.cpp --------------------------
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. -------------------------------------------------------
  126. -- If I add this it does not break
  127. -------------------------------------------------------
  128.  
  129.  
  130.  
  131. class mqmessagebridge: public QObject {
  132.     Q_OBJECT
  133.     MQMessageManager *manager;
  134.     QThread *thread;
  135. public:
  136.     mqmessagebridge(MQMessageManager* mq_, QThread *_currThread) : manager(mq_), thread(_currThread) {}
  137.  
  138. public slots:
  139.   void processMessage(SG64ZeroMQ* mq, ThriftStruct* message, GError **error) {
  140.       qDebug("Thread of the bridge %x", QThread::currentThread()); // Thread of the bridge c00024f0
  141.       qDebug("Thread of the stored class in bridge %x", thread);   // Thread of the stored class in bridge d400eb80
  142. //      manager->moveToThread(QThread::currentThread()); //  QObject::moveToThread: Cannot move objects with a parent
  143.       manager->processMessage(mq,message,error);
  144.   }
  145. };
  146.  
  147.  
  148.  
  149. --------  in mqmessagemanager.cpp --------------------------
  150.  
  151. // This is the static wrapper for the class
  152. // The class comes from a gpointer (void*) data
  153. // And it's converted to a class pointer.
  154. // Then we just simply call a method.
  155.  
  156. // Since the call back comes from glib is a plain pthread thread.
  157.  
  158. static gboolean callbackWrapper(SG64ZeroMQ* mq, ThriftStruct* message, gpointer data, GError **error) {
  159.     qDebug("Message received");
  160.     mqmessagebridge *self = static_cast<mqmessagebridge*>(data);
  161.     fflush(stdout);
  162.     qDebug("Thread of the call %x", QThread::currentThread());
  163.  
  164.     self->processMessage(mq, message, error);
  165.     return TRUE;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement