messagequeue.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "messagequeue.h"
  2. #include "qdebug.h"
  3. #include <QApplication>
  4. #include <QMessageBox>
  5. void MessageQueue::processQueue()
  6. {
  7. if (isProcessing || messageQueue.isEmpty()) {
  8. return;
  9. }
  10. isProcessing = true;
  11. QString currentMessage = messageQueue.dequeue();
  12. QVariant messageBoxPointX = this->messageBoxPointX;
  13. QVariant messageBoxPointY = this->messageBoxPointY;
  14. QVariant messageTitle = this->messageTitle;
  15. qDebug() << "currentMessage" << currentMessage;
  16. // 在主线程中显示消息框
  17. QMetaObject::invokeMethod(
  18. QApplication::instance(),
  19. [this, currentMessage, messageBoxPointX, messageBoxPointY, messageTitle]() {
  20. bool isokX = false;
  21. bool isokY = false;
  22. int x = messageBoxPointX.toInt(&isokX);
  23. int y = messageBoxPointY.toInt(&isokY);
  24. qDebug() << x << y << isokX << isokY;
  25. QMessageBox *messageBox = new QMessageBox;
  26. if (isokX && isokY) {
  27. messageBox->move(x, y);
  28. }
  29. QString title = messageTitle.toString();
  30. if (title.isEmpty()) {
  31. title = tr("Tips");
  32. }
  33. messageBox->setWindowModality(Qt::NonModal);
  34. messageBox->setText(currentMessage);
  35. messageBox->setWindowTitle(title);
  36. connect(messageBox, &QMessageBox::finished, this, &MessageQueue::onMessageBoxClosed);
  37. messageBox->show();
  38. },
  39. Qt::QueuedConnection);
  40. }