Files
QT_wuhan_YT/widgets/numpaddialog.cpp
T

159 lines
5.3 KiB
C++

#include "numpaddialog.h"
#include <QVBoxLayout>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#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(600, 730);
panel->setStyleSheet(QString(
"QWidget#NumPadPanel{background-color:%1;border:4px solid %2;border-radius:16px;}"
"QPushButton[numpad=\"true\"]{background-color:#2D3947;color:#E6EDF3;border:1px solid #3D4A59;border-radius:12px;font-size:40px;}"
"QPushButton[numpad=\"true\"]:pressed{background-color:#2E7BE6;color:#FFFFFF;}"
"QPushButton[numpad=\"true\"]:focus{background-color:#3D8BFD;color:#FFFFFF;border:2px solid #8ABFFF;}"
"QPushButton[numpad=\"true\"][cancel=\"true\"]:focus{background-color:#E5484D;color:#FFFFFF;border:2px solid #FFA6A9;}")
.arg(C::PANEL, C::BORDER));
QVBoxLayout* panelLay = new QVBoxLayout(panel);
panelLay->setContentsMargins(24, 40, 24, 50);
panelLay->setSpacing(50);
// 输入回显
m_echo = new QLineEdit(panel);
m_echo->setReadOnly(true);
m_echo->setEchoMode(QLineEdit::Password);
m_echo->setAlignment(Qt::AlignCenter);
m_echo->setFixedSize(510, 100);
m_echo->setStyleSheet("font-size:70px;");
m_echo->setFocusPolicy(Qt::NoFocus); // 纯显示回显:不参与 Tab 选中
m_echo->setAttribute(Qt::WA_TransparentForMouseEvents); // 且点击穿透,不响应鼠标
panelLay->addWidget(m_echo, 0, Qt::AlignHCenter);
// 数字键区(1-9, 0, 退格, 清除)
QGridLayout* grid = new QGridLayout();
grid->setSpacing(30);
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->setStyleSheet("font-size:50px;");
b->setFixedSize(150, 120);
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->setStyleSheet("font-size:50px;");
b0->setFixedSize(150, 120);
connect(b0, &QPushButton::clicked, this, [this](){ onDigit("0"); });
grid->addWidget(b0, 3, 1);
QPushButton* bBack = new QPushButton("退格", panel);
bBack->setProperty("numpad", true);
bBack->setStyleSheet("font-size:40px;");
bBack->setFixedSize(150, 120);
connect(bBack, &QPushButton::clicked, this, &NumPadDialog::onBackspace);
grid->addWidget(bBack, 3, 0);
QPushButton* bClear = new QPushButton("清除", panel);
bClear->setProperty("numpad", true);
bClear->setStyleSheet("font-size:40px;");
bClear->setFixedSize(150, 108);
connect(bClear, &QPushButton::clicked, this, &NumPadDialog::onClear);
grid->addWidget(bClear, 3, 2);
// 确认 / 取消(与数字键一致的灰底,选中时发亮)
QPushButton* bOk = new QPushButton("确认", panel);
bOk->setProperty("numpad", true);
bOk->setStyleSheet("font-size:60px;");
bOk->setFixedSize(160, 140);
connect(bOk, &QPushButton::clicked, this, &NumPadDialog::accept);
QPushButton* bCancel = new QPushButton("取消", panel);
bCancel->setProperty("numpad", true);
bCancel->setProperty("cancel", true); // 取消语义:选中时红底白字
bCancel->setStyleSheet("font-size:60px;");
bCancel->setFixedSize(160, 140);
connect(bCancel, &QPushButton::clicked, this, &NumPadDialog::reject);
// 确认 / 取消:单独一行、水平居中,并与数字键盘留出间隔
QHBoxLayout* okRow = new QHBoxLayout();
okRow->setSpacing(80);
okRow->addStretch(1);
okRow->addWidget(bOk);
okRow->addWidget(bCancel);
okRow->addStretch(1);
panelLay->addLayout(grid);
panelLay->setAlignment(grid, Qt::AlignHCenter);
//panelLay->addSpacing(20);
panelLay->addLayout(okRow);
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());
}