| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- #include "messagequeue.h"
- #include "qdebug.h"
- #include <QApplication>
- #include <QMessageBox>
- static bool isNumeric(const QVariant &variant)
- {
- int type = variant.type();
- return (type == QMetaType::Int || type == QMetaType::UInt || type == QMetaType::Double
- || type == QMetaType::Float || type == QMetaType::Long || type == QMetaType::ULong
- || type == QMetaType::LongLong || type == QMetaType::ULongLong);
- }
- void MessageQueue::processQueue()
- {
- if (isProcessing || messageQueue.isEmpty()) {
- return;
- }
- isProcessing = true;
- QString currentMessage = messageQueue.dequeue();
- QVariant messageBoxPointX = this->messageBoxPointX;
- QVariant messageBoxPointY = this->messageBoxPointY;
- QVariant messageTitle = this->messageTitle;
- qDebug() << "currentMessage" << currentMessage << messageBoxPointX << messageBoxPointY
- << messageTitle;
- // 在主线程中显示消息框
- QMetaObject::invokeMethod(
- QApplication::instance(),
- [this, currentMessage, messageBoxPointX, messageBoxPointY, messageTitle]() {
- bool isokX = false;
- bool isokY = false;
- int x = messageBoxPointX.toInt(&isokX);
- int y = messageBoxPointY.toInt(&isokY);
- qDebug() << x << y << isokX << isokY;
- QMessageBox *messageBox = new QMessageBox;
- if (isokX && isokY && isNumeric(messageBoxPointX) && isNumeric(messageBoxPointY)) {
- messageBox->move(x, y);
- }
- QString title = messageTitle.toString();
- if (title.isEmpty()) {
- title = tr("Tips");
- }
- messageBox->setWindowModality(Qt::NonModal);
- messageBox->setText(currentMessage);
- messageBox->setWindowTitle(title);
- connect(messageBox, &QMessageBox::finished, this, &MessageQueue::onMessageBoxClosed);
- messageBox->show();
- },
- Qt::QueuedConnection);
- }
|