Skip to content

Commit 0cc119e

Browse files
authored
[202_84] 按Enter可以确认,按ESC可以取消 (#2383)
1 parent 0310c47 commit 0cc119e

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

devel/202_84.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
注:社区版不支持markdown与OCR功能,故虽有选项但无法使用。
1717

18+
## 2025/12/17 按Enter可以确认,按ESC可以取消
19+
1820
## 2025/12/27
1921

2022
### What

src/Plugins/Qt/QTMWindow.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "tm_window.hpp"
1717

1818
#include <QCloseEvent>
19+
#include <QKeyEvent>
20+
#include <QPushButton>
1921
#include <QSettings>
2022

2123
void
@@ -44,6 +46,61 @@ QTMPlainWindow::resizeEvent (QResizeEvent* event) {
4446
QWidget::resizeEvent (event);
4547
}
4648

49+
void
50+
QTMPlainWindow::keyPressEvent (QKeyEvent* event) {
51+
if (DEBUG_QT_WIDGETS)
52+
debug_widgets << "QTMPlainWindow key press: " << event->key () << LF;
53+
54+
if (event->key () == Qt::Key_Escape) {
55+
// ESC键:关闭对话框
56+
if (DEBUG_QT_WIDGETS) debug_widgets << "ESC pressed, closing window" << LF;
57+
event->accept ();
58+
close ();
59+
}
60+
else if (event->key () == Qt::Key_Return || event->key () == Qt::Key_Enter) {
61+
// Enter键:尝试找到默认按钮并点击
62+
if (DEBUG_QT_WIDGETS)
63+
debug_widgets << "Enter pressed, looking for default button" << LF;
64+
event->accept ();
65+
66+
// 查找对话框中的按钮
67+
QList<QPushButton*> buttons= findChildren<QPushButton*> ();
68+
for (QPushButton* button : buttons) {
69+
if (button->isDefault () ||
70+
button->text ().contains ("Ok", Qt::CaseInsensitive)) {
71+
if (DEBUG_QT_WIDGETS)
72+
debug_widgets << "Found button: " << from_qstring (button->text ())
73+
<< LF;
74+
button->click ();
75+
return;
76+
}
77+
}
78+
79+
// 如果没有找到默认按钮,尝试点击第一个"Ok"按钮
80+
for (QPushButton* button : buttons) {
81+
if (button->text ().contains ("Ok", Qt::CaseInsensitive)) {
82+
if (DEBUG_QT_WIDGETS)
83+
debug_widgets << "Found Ok button: " << from_qstring (button->text ())
84+
<< LF;
85+
button->click ();
86+
return;
87+
}
88+
}
89+
90+
// 如果还没有找到,点击第一个按钮
91+
if (!buttons.isEmpty ()) {
92+
if (DEBUG_QT_WIDGETS)
93+
debug_widgets << "Clicking first button: "
94+
<< from_qstring (buttons.first ()->text ()) << LF;
95+
buttons.first ()->click ();
96+
}
97+
}
98+
else {
99+
// 其他按键传递给父类处理
100+
QWidget::keyPressEvent (event);
101+
}
102+
}
103+
47104
void
48105
QTMWindow::closeEvent (QCloseEvent* event) {
49106
widget tmwid= qt_window_widget_rep::widget_from_qwidget (this);

src/Plugins/Qt/QTMWindow.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef QTMWINDOW_HPP
1313
#define QTMWINDOW_HPP
1414

15+
#include <QKeyEvent>
1516
#include <QMainWindow>
1617

1718
#include "qt_tm_widget.hpp"
@@ -40,6 +41,8 @@ class QTMPlainWindow : public QWidget {
4041
public:
4142
QTMPlainWindow (QWidget* parent) : QWidget (parent) {
4243
if (DEBUG_QT) debug_qt << "Creating QTMPlainWindow" << LF;
44+
// 设置焦点策略以接收键盘事件
45+
setFocusPolicy (Qt::StrongFocus);
4346
}
4447
virtual ~QTMPlainWindow () {
4548
if (DEBUG_QT) debug_qt << "Deleting QTMPlainWindow" << LF;
@@ -52,6 +55,7 @@ class QTMPlainWindow : public QWidget {
5255
virtual void closeEvent (QCloseEvent* event);
5356
virtual void moveEvent (QMoveEvent* event);
5457
virtual void resizeEvent (QResizeEvent* event);
58+
virtual void keyPressEvent (QKeyEvent* event) override;
5559
};
5660

5761
/*! The underlying QWidget for a qt_tm_widget_rep.

0 commit comments

Comments
 (0)