-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualKeyboardWidget.h
More file actions
87 lines (71 loc) · 2.5 KB
/
VirtualKeyboardWidget.h
File metadata and controls
87 lines (71 loc) · 2.5 KB
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
#ifndef VIRTUALKEYBOARDWIDGET_H
#define VIRTUALKEYBOARDWIDGET_H
#include "qevent.h"
#include <QLineEdit>
#include <QPushButton>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QWidget>
class VirtualKeyboardWidget : public QWidget {
Q_OBJECT
public:
explicit VirtualKeyboardWidget(QWidget *parent = nullptr);
explicit VirtualKeyboardWidget(const QString &defaultText, const QString &placeholderText = "", QWidget *parent = nullptr);
QString text() const;
void setText(const QString &text);
void setPlaceholderText(const QString &text);
signals:
void textEntered(const QString &text);
void cancelled();
private:
enum KeyboardPage { Letters, Numbers, Symbols };
KeyboardPage currentPage;
QLineEdit *inputLine;
QVBoxLayout *keyLayout;
QWidget *keyboardWidget;
QScrollArea *scrollArea;
QVector<QStringList> letterKeys;
QVector<QStringList> letterKeysUpper; // 大写字母
QVector<QStringList> numberKeys;
QVector<QStringList> symbolKeys;
bool isUpperCase; // 当前是否为大写模式
QPushButton *caseToggleBtn; // 大小写切换按钮
QPushButton *clearButton; // 清空按钮
void setupUI();
void initializeKeyboard(const QString &defaultText, const QString &placeholderText);
void buildKeyboard();
QPushButton *createButton(const QString &text);
void toggleCase(); // 切换大小写
QVector<QVector<QPushButton *>> keyButtons; // 用于复用按钮
const int maxRows = 7;
const int maxCols = 5;
};
class MyButton : public QPushButton {
QPoint pressPos;
bool moved = false;
bool pressedInside = false;
public:
explicit MyButton(const QString &text, QWidget *parent = nullptr)
: QPushButton(text, parent) {}
protected:
void mousePressEvent(QMouseEvent *event) override {
pressPos = event->pos();
moved = false;
pressedInside = rect().contains(pressPos);
// 不立即传给父类,先判断是否滑动
// 不调用 QPushButton::mousePressEvent(event);
}
void mouseMoveEvent(QMouseEvent *event) override {
if ((event->pos() - pressPos).manhattanLength() > 10) {
moved = true;
}
// 不传给父类,避免滑动中触发点击动画
}
void mouseReleaseEvent(QMouseEvent *event) override {
if (!moved && pressedInside && rect().contains(event->pos())) {
emit clicked(); // 只在无滑动且点击范围内才触发
}
// 不传给 QPushButton::mouseReleaseEvent(event);
}
};
#endif // VIRTUALKEYBOARDWIDGET_H