Files
QT_wuhan_YT/widgets/confirmdialog.cpp
T
2026-09-09 17:00:37 +08:00

97 lines
3.2 KiB
C++

#include "confirmdialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTimer>
#include "global.h"
ConfirmDialog::ConfirmDialog(QWidget* parent)
: QDialog(parent)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
setAttribute(Qt::WA_TranslucentBackground);
setModal(true);
if (parent) setGeometry(parent->rect());
setStyleSheet("background-color: rgba(0,0,0,102);");
QWidget* panel = new QWidget(this);
panel->setObjectName("ConfirmPanel");
panel->setFixedSize(560, 260);
panel->setStyleSheet(QString(
"QWidget#ConfirmPanel{background-color:%1;border:2px solid %2;border-radius:16px;}")
.arg(C::PANEL, C::BORDER));
QVBoxLayout* lay = new QVBoxLayout(panel);
lay->setContentsMargins(28, 24, 28, 24);
lay->setSpacing(16);
m_title = new QLabel("", panel);
m_title->setStyleSheet(QString("color:%1;font-size:28px;font-weight:700;").arg(C::TEXT));
m_title->setAlignment(Qt::AlignCenter);
lay->addWidget(m_title);
m_body = new QLabel("", panel);
m_body->setStyleSheet(QString("color:%1;font-size:22px;").arg(C::TEXT));
m_body->setAlignment(Qt::AlignCenter);
m_body->setWordWrap(true);
lay->addWidget(m_body, 1);
QHBoxLayout* btnLay = new QHBoxLayout();
btnLay->addStretch(1);
QPushButton* ok = new QPushButton("确认", panel);
ok->setProperty("variant", "primary");
ok->setFixedSize(160, 72);
connect(ok, &QPushButton::clicked, this, &ConfirmDialog::accept);
QPushButton* cancel = new QPushButton("取消", panel);
cancel->setFixedSize(160, 72);
connect(cancel, &QPushButton::clicked, this, &ConfirmDialog::reject);
btnLay->addWidget(ok);
btnLay->addWidget(cancel);
btnLay->addStretch(1);
lay->addLayout(btnLay);
QVBoxLayout* root = new QVBoxLayout(this);
root->setContentsMargins(0, 0, 0, 0);
root->addStretch(1);
root->addWidget(panel, 0, Qt::AlignHCenter);
root->addStretch(1);
}
void ConfirmDialog::setTitle(const QString& t) { m_title->setText(t); }
void ConfirmDialog::setText(const QString& t) { m_body->setText(t); }
bool ConfirmDialog::confirm(QWidget* parent, const QString& title, const QString& text)
{
ConfirmDialog dlg(parent);
dlg.setTitle(title);
dlg.setText(text);
return dlg.exec() == QDialog::Accepted;
}
void ConfirmDialog::toast(QWidget* parent, const QString& text, int ms)
{
QDialog* d = new QDialog(parent);
d->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
d->setAttribute(Qt::WA_TranslucentBackground);
d->setModal(true);
if (parent) d->setGeometry(parent->rect());
d->setStyleSheet("background-color: rgba(0,0,0,80);");
QLabel* lbl = new QLabel(text, d);
lbl->setAlignment(Qt::AlignCenter);
lbl->setStyleSheet(QString(
"background-color:%1;color:%2;font-size:28px;font-weight:700;border:2px solid %3;border-radius:16px;padding:32px 48px;")
.arg(C::PANEL, C::SUCCESS, C::BORDER));
lbl->adjustSize();
QVBoxLayout* root = new QVBoxLayout(d);
root->addStretch(1);
root->addWidget(lbl, 0, Qt::AlignHCenter);
root->addStretch(1);
QTimer::singleShot(ms, d, &QDialog::accept);
d->exec();
d->deleteLater();
}