1. 程式人生 > >QT 實現彈出提示框定時消失

QT 實現彈出提示框定時消失

#include "dialog.h"
#include "ui_dialog.h"
#include <QLabel>
#include <QTimer>
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog),
    label(new QLabel(this))
{
    ui->setupUi(this);
}
Dialog::~Dialog()
{
    delete ui;
}
void Dialog::on_toolButton_clicked()
{
    showFinished();
    QTimer::singleShot(3000, this, SLOT(slotHideFinishedLabel()));  // 這裡是一個3秒定時器, 且只執行一次。
}
//下面這是提示框隱藏
void Dialog::slotHideFinishedLabel()
{
    label->hide();
    this->close();
}
//下面這個紅色提示框顯示
void Dialog::showFinished()
{
    QRect rect = geometry();
    label->setMaximumWidth(500);
    label->setMaximumHeight(50);
    label->setMinimumWidth(500);
    label->setMinimumHeight(50);
    QFont font;
    font.setPointSize(25);
    label->setFont(font);
    label->setStyleSheet(QLatin1String("color:red;"));
    label->setText("The answer end of time!");
    label->setGeometry(int((rect.width()-label->width())/2), int((rect.height()-label->height())/2), label->width(), label->height());
    label->show();
}