messagequeue.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifndef MESSAGEQUEUE_H
  2. #define MESSAGEQUEUE_H
  3. #include <QObject>
  4. #include <QQueue>
  5. #include <QVariant>
  6. #include "qdebug.h"
  7. class MessageQueue : public QObject
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit MessageQueue(QObject *parent = nullptr)
  12. : QObject(parent)
  13. , isProcessing(false)
  14. {}
  15. // 向队列中添加消息
  16. void addMessage(const QString &text,
  17. const QVariant &messageBoxPointX,
  18. const QVariant &messageBoxPointY,
  19. const QVariant &messageTitle)
  20. {
  21. qDebug() << text << messageBoxPointX << messageBoxPointY << messageTitle;
  22. this->messageBoxPointX = messageBoxPointX;
  23. this->messageBoxPointY = messageBoxPointY;
  24. this->messageTitle = messageTitle;
  25. messageQueue.enqueue(text);
  26. processQueue();
  27. }
  28. private:
  29. QQueue<QString> messageQueue;
  30. bool isProcessing;
  31. // 处理队列中的消息
  32. void processQueue();
  33. QVariant messageBoxPointX;
  34. QVariant messageBoxPointY;
  35. QVariant messageTitle;
  36. private slots:
  37. // 当消息框关闭时,继续处理队列中的下一个消息
  38. void onMessageBoxClosed()
  39. {
  40. isProcessing = false;
  41. processQueue();
  42. }
  43. };
  44. #endif // MESSAGEQUEUE_H