#pragma execution_character_set("utf-8") #include "countdownmessagebox.h" CountdownMessageBox::CountdownMessageBox(int timeoutSeconds, QWidget *parent) : QMessageBox(parent) , m_timeoutSeconds(timeoutSeconds) { // 初始显示时间 setText(tr("还有%1秒关闭...").arg(m_timeoutSeconds)); // 创建定时器 m_timer = new QTimer(this); connect(m_timer, &QTimer::timeout, this, &CountdownMessageBox::updateCountdown); m_timer->start(1000); // 每秒触发一次 } void CountdownMessageBox::showCountdownMessageBox(QWidget *parent, int timeoutSeconds, const QString &title) { CountdownMessageBox msgBox(timeoutSeconds, parent); msgBox.setWindowTitle(title); msgBox.setStandardButtons(QMessageBox::Ok); msgBox.exec(); } void CountdownMessageBox::updateCountdown() { if (m_timeoutSeconds > 0) { --m_timeoutSeconds; setText(tr("还有%1秒关闭...").arg(m_timeoutSeconds)); } else { m_timer->stop(); accept(); // 关闭对话框 } }