1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| #include "key_pad.h" #include <QBoxLayout> #include <QDateTime> #include <QRandomGenerator> #include <QFile> #include <QLatin1StringView>
#define QSS_STYLE_IMAGE ":/styles/style_image.qss" #define QSS_STYLE_QSS ":/styles/style_qss.qss"
namespace { const QList<KeyInfo> pads = { {"1", 1, ":/images/1.png"}, {"2", 2, ":/images/2.png"}, {"3", 3, ":/images/3.png"}, {"4", 4, ":/images/4.png"}, {"5", 5, ":/images/5.png"}, {"6", 6, ":/images/6.png"}, {"7", 7, ":/images/7.png"}, {"8", 8, ":/images/8.png"}, {"9", 9, ":/images/9.png"}, {"⌫", 11, ":/images/del.png"}, {"0", 0, ":/images/0.png"}, {"✓", 12, ":/images/ok.png"} }; }
KeyPad::KeyPad(QWidget *parent, PadMode mode) : QWidget(parent), m_mode(mode) { this->setGeometry(50, 100, 240, 400);
auto main_layout = new QVBoxLayout(this); main_layout->setSpacing(0); main_layout->setContentsMargins(0, 0, 0, 0);
lcdShow = new QLCDNumber(this); lcdShow->setDigitCount(6); lcdShow->display(""); lcdShow->setSegmentStyle(QLCDNumber::Flat); auto lcdContainer = new QWidget(this); lcdContainer->setMinimumSize(240, 80); lcdContainer->setMaximumSize(240, 80); auto containerLayout = new QVBoxLayout(lcdContainer); if (m_mode == PadMode::ImageMode) { containerLayout->setContentsMargins(15, 15, 15, 5); } else { containerLayout->setContentsMargins(0, 0, 0, 0); } containerLayout->addWidget(lcdShow);
padWidget = new QWidget(this); padLayout = new QGridLayout(padWidget); padLayout->setContentsMargins(0, 0, 0, 0); padLayout->setSpacing(0);
init_keypad(); loadStyleSheet(); random_keypad();
main_layout->addWidget(lcdContainer); main_layout->addWidget(padWidget); main_layout->setStretch(0, 1); main_layout->setStretch(1, 4); }
KeyPad::~KeyPad() {}
void KeyPad::loadStyleSheet() { QString stylePath = (m_mode == PadMode::ImageMode) ? QSS_STYLE_IMAGE : QSS_STYLE_QSS; QFile file(stylePath); if (file.open(QFile::ReadOnly)) { this->setStyleSheet(QLatin1String(file.readAll())); file.close(); } }
void KeyPad::init_keypad() { QPushButton *btn;
for (int x = 0; x < 4; ++x) { for (int y = 0; y < 3; ++y) { int pos = x * 3 + y; btn = new QPushButton(padWidget); btn->setProperty("value", pads[pos].value); btn->setMinimumSize(80, 80); btn->setMaximumSize(80, 80);
if (m_mode == 1) { btn->setIcon(QIcon(pads[pos].imagePath)); btn->setIconSize(QSize(50, 50)); } else { btn->setText(pads[pos].text); if (pads[pos].value >= DeleteBtn) { btn->setProperty("btnType", "func"); } else { btn->setProperty("btnType", "num"); } } connect(btn, &QPushButton::clicked, this, &KeyPad::lcdChange); m_btns.append(btn); } } }
void KeyPad::random_keypad() { auto random = QRandomGenerator::global(); QList<int> indexes; for (int i = 0; i < pads.size(); ++i) { if (pads[i].value <= KeyRole::Num9) { indexes.append(i); } } std::shuffle(indexes.begin(), indexes.end(), *random);
int pos_indes = 0; for (int x = 0; x < 4; ++x) { for (int y = 0; y < 3; ++y) { int pos = x * 3 + y; QPushButton *btn; if (pads[pos].value >= KeyRole::DeleteBtn) { btn = m_btns[pos]; } else { btn = m_btns[indexes[pos_indes]]; ++pos_indes; } padLayout->addWidget(btn, x, y, 1, 1); } } }
void KeyPad::lcdChange() { auto btn = qobject_cast<QPushButton *>(sender()); if (btn == nullptr) { return; } int v = btn->property("value").toInt(); if (v == KeyRole::DeleteBtn) { if (m_currentInput.isEmpty()) { return; } m_currentInput.chop(1); lcdShow->display(m_currentInput.isEmpty() ? "" : m_currentInput); } else if (v == KeyRole::ConfirmBtn) { m_currentInput.clear(); lcdShow->display(""); } else { if (m_currentInput.size() >= 6) { m_currentInput.clear(); }
m_currentInput.append(QString::number(v)); lcdShow->display(m_currentInput);
if (m_currentInput.size() == 6) { random_keypad(); } } }
|