#include "messagebox.h" #include #include #include #include #include #include #include #include #include #include #include #include #include MessageBox::MessageBox(QWidget *parent, const QString &text, Button button) : QWidget(parent) { this->resize(300, 200); m_pLoop = new QEventLoop(this); QLabel *label =new QLabel; label->setTextFormat(Qt::RichText); label->setWordWrap(true); label->setAlignment(Qt::AlignCenter); label->setText(text); QVBoxLayout *VLayout = new QVBoxLayout(this); VLayout->addSpacing(30); VLayout->addWidget(label); QHBoxLayout *hLayout = new QHBoxLayout; QPushButton *yes = new QPushButton(tr("OK"), this); connect(yes, &QPushButton::clicked, this, [=](){ m_pLoop->exit(Button::yes); }); QPushButton *no = new QPushButton(tr("Cancel"), this); connect(no, &QPushButton::clicked, this, [=](){ m_pLoop->exit(Button::no); }); switch (button) { case Button::yes: { yes->setVisible(true); no->setVisible(false); } break; case Button::no: { yes->setVisible(false); no->setVisible(false); } break; case Button::yes_no: { yes->setVisible(true); yes->setVisible(true); }break; default: break; } hLayout->addStretch(); hLayout->addWidget(yes); hLayout->addWidget(no); VLayout->addLayout(hLayout); setAttribute(Qt::WA_DeleteOnClose); } MessageBox::MessageBox(QWidget *parent):QWidget(parent) { setAttribute(Qt::WA_DeleteOnClose); } int MessageBox::exec() { QWidget *parentWidget = dynamic_cast(parent()); if(parentWidget != Q_NULLPTR) { int x = (parentWidget->width()-width())/2; int y = (parentWidget->height()-height())/2; move(x, y); filterChild(parentWidget); }else{ this->setWindowFlags(Qt::FramelessWindowHint); int x = (qApp->desktop()->width()-width())/2; int y = (qApp->desktop()->height()-height())/2; move(x, y); } this->setVisible(true); int ret = m_pLoop->exec(); if(parentWidget != Q_NULLPTR) { //解除其它窗口部件操作. foreach (QObject *o, m_object) { o->removeEventFilter(this); } m_object.clear(); } delete m_pLoop; return ret; } void MessageBox::paintEvent(QPaintEvent *e) { Q_UNUSED(e) QWidget::paintEvent(e); QPainter painter(this); painter.fillRect(this->rect(), QColor(251, 251, 251)); painter.setPen(Qt::blue); painter.drawText(20, 20, "Message"); QPen pen(Qt::blue); pen.setWidthF(2.0); painter.setPen(pen); painter.drawLine(QPoint(20, 25), QPoint(this->width()-20, 25)); } bool MessageBox::eventFilter(QObject *watched, QEvent *event) { //过滤所非消息窗口的鼠标点击和聚集事件,使只能操作当强消息窗口. QInputEvent *pMouse = dynamic_cast(event); QFocusEvent *pFocus = dynamic_cast(event); if(m_object.contains(watched) && (pMouse|| pFocus)) { event->ignore(); return true; } return QWidget::eventFilter(watched, event); } void MessageBox::filterChild(QObject *o) { foreach (QObject *child, o->children()) { if( child !=this) { m_object << child; child->installEventFilter(this); filterChild(child); } } } int MessageBox::showMessage(QWidget *parent, const QString &text, Button button) { MessageBox tip(parent, text, button); return tip.exec(); } int MessageBox::showDuringMessage(QWidget *parent, const QString &text, int duringSeconds) { MessageBox tip(parent, text, MessageBox::no); QPropertyAnimation pro(&tip, "windowOpacity"); pro.setStartValue(0.1); pro.setEndValue(1.0); pro.setDuration(duringSeconds*1000); connect(&pro, &QPropertyAnimation::finished, &tip, [=, &tip](){ tip.m_pLoop->exit();; }); pro.start(); return tip.exec(); }