#include "numpaddialog.h" #include #include #include #include #include #include #include "global.h" NumPadDialog::NumPadDialog(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("NumPadPanel"); panel->setFixedSize(960, 486); panel->setStyleSheet(QString( "QWidget#NumPadPanel{background-color:%1;border:2px solid %2;border-radius:16px;}") .arg(C::PANEL, C::BORDER)); QVBoxLayout* panelLay = new QVBoxLayout(panel); panelLay->setContentsMargins(24, 24, 24, 24); panelLay->setSpacing(16); // 输入回显 m_echo = new QLineEdit(panel); m_echo->setReadOnly(true); m_echo->setEchoMode(QLineEdit::Password); m_echo->setAlignment(Qt::AlignCenter); m_echo->setFixedHeight(64); panelLay->addWidget(m_echo); // 数字键区(1-9, 0, 退格, 清除) QGridLayout* grid = new QGridLayout(); grid->setSpacing(10); const QString digits[3][3] = {{"1","2","3"},{"4","5","6"},{"7","8","9"}}; for (int r = 0; r < 3; ++r) for (int c = 0; c < 3; ++c) { QPushButton* b = new QPushButton(digits[r][c], panel); b->setProperty("numpad", true); b->setFixedSize(130, 98); connect(b, &QPushButton::clicked, this, [this, d = digits[r][c]](){ onDigit(d); }); grid->addWidget(b, r, c); } QPushButton* b0 = new QPushButton("0", panel); b0->setProperty("numpad", true); b0->setFixedSize(130, 98); connect(b0, &QPushButton::clicked, this, [this](){ onDigit("0"); }); grid->addWidget(b0, 3, 0); QPushButton* bBack = new QPushButton("退格", panel); bBack->setProperty("numpad", true); bBack->setStyleSheet("font-size:28px;background-color:#2D3947;color:#E6EDF3;border:1px solid #3D4A59;border-radius:12px;"); bBack->setFixedSize(130, 98); connect(bBack, &QPushButton::clicked, this, &NumPadDialog::onBackspace); grid->addWidget(bBack, 3, 1); QPushButton* bClear = new QPushButton("清除", panel); bClear->setProperty("numpad", true); bClear->setStyleSheet("font-size:28px;background-color:#2D3947;color:#E6EDF3;border:1px solid #3D4A59;border-radius:12px;"); bClear->setFixedSize(130, 98); connect(bClear, &QPushButton::clicked, this, &NumPadDialog::onClear); grid->addWidget(bClear, 3, 2); // 确认 / 取消 QPushButton* bOk = new QPushButton("确认", panel); bOk->setProperty("variant", "primary"); bOk->setFixedSize(130, 98); connect(bOk, &QPushButton::clicked, this, &NumPadDialog::accept); QPushButton* bCancel = new QPushButton("取消", panel); bCancel->setFixedSize(130, 98); connect(bCancel, &QPushButton::clicked, this, &NumPadDialog::reject); grid->addWidget(bOk, 3, 3); grid->addWidget(bCancel, 3, 4); grid->setColumnStretch(4, 0); grid->setColumnStretch(5, 1); panelLay->addLayout(grid); panelLay->addStretch(1); // 居中放置面板 QVBoxLayout* root = new QVBoxLayout(this); root->setContentsMargins(0, 0, 0, 0); root->addStretch(1); root->addWidget(panel, 0, Qt::AlignHCenter); root->addStretch(1); refreshEcho(); } void NumPadDialog::setValue(const QString& v) { m_value = v.left(m_maxLen); refreshEcho(); } void NumPadDialog::clear() { m_value.clear(); refreshEcho(); } void NumPadDialog::onDigit(const QString& d) { if (m_value.size() >= m_maxLen) return; // 超过最大位数忽略 m_value += d; refreshEcho(); } void NumPadDialog::onBackspace() { if (!m_value.isEmpty()) m_value.chop(1); refreshEcho(); } void NumPadDialog::onClear() { m_value.clear(); refreshEcho(); } void NumPadDialog::refreshEcho() { m_echo->setText(m_value); m_echo->setCursorPosition(m_value.size()); }