Skip to content

Commit 46fc456

Browse files
committed
feat:优化国家下拉框的补全交互
1 parent ee96491 commit 46fc456

File tree

4 files changed

+798
-700
lines changed

4 files changed

+798
-700
lines changed

src/gameSettings.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from country_name import country_name
66
from PyQt5.QtGui import QPixmap
77
from PyQt5.QtCore import Qt
8+
from PyQt5.QtCore import QSortFilterProxyModel
9+
810

911
class ui_Form(Ui_Form):
1012
def __init__(self, mainWindow):
@@ -43,9 +45,12 @@ def __init__(self, mainWindow):
4345
self.pushButton_yes.clicked.connect(self.processParameter)
4446
self.pushButton_no.clicked.connect(self.Dialog.close)
4547
# self.comboBox_country.activated['QString'].connect(lambda x: self.onchange_combobox_country(x))
46-
self.comboBox_country.editTextChanged.connect(self.onchange_combobox_country)
47-
self.comboBox_country.lineEdit().setAlignment(Qt.AlignCenter)
4848
self.country_name = list(country_name.keys())
49+
self.comboBox_country.lineEdit().setAlignment(Qt.AlignCenter)
50+
self.comboBox_country.editTextChanged.connect(self.onchange_combobox_country)
51+
self.comboBox_country.lineEdit().setText(self.country)
52+
53+
4954
self.setParameter()
5055

5156
def set_country_flag(self, flag_name):
@@ -66,12 +71,17 @@ def onchange_combobox_country(self, qtext):
6671
# 记录光标位置
6772
line_edit = self.comboBox_country.lineEdit()
6873
cursor_position = line_edit.cursorPosition()
69-
self.comboBox_country.editTextChanged.disconnect(self.onchange_combobox_country)
74+
75+
self.comboBox_country.blockSignals(True) # 避免清空时再次触发信号
7076
self.comboBox_country.clear()
71-
self.comboBox_country.addItems(filter(lambda x: qtext in x, self.country_name))
72-
self.comboBox_country.setCurrentText(qtext)
77+
# 过滤逻辑:只保留包含输入的国家(不区分大小写)
78+
filtered = [c for c in self.country_name if qtext.lower() in c.lower()]
79+
# 重新填充
80+
self.comboBox_country.addItems(filtered)
81+
# 恢复用户已输入的文本
82+
self.comboBox_country.setEditText(qtext)
7383
line_edit.setCursorPosition(cursor_position)
74-
self.comboBox_country.editTextChanged.connect(self.onchange_combobox_country)
84+
self.comboBox_country.blockSignals(False)
7585
self.set_country_flag(qtext)
7686

7787
def setParameter(self):
@@ -207,3 +217,5 @@ def processParameter(self):
207217

208218

209219

220+
221+

src/ui/uiComponents.py

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# from PyQt5.QtGui import QPainter, QPen, QBrush, QColor, QFont, QImage, QPainterPath
1313
from PyQt5.QtGui import QBrush, QColor
1414
from PyQt5.QtGui import QPixmap
15-
import configparser
16-
from PyQt5.QtCore import pyqtSignal
17-
from PyQt5.QtCore import QEvent
15+
from PyQt5.QtCore import pyqtSignal
16+
from PyQt5.QtWidgets import QVBoxLayout, QCompleter, QLineEdit
17+
from PyQt5.QtCore import QStringListModel, QSortFilterProxyModel, QRegExp
1818
# ui相关的小组件,非窗口
1919

2020
class RoundQDialog(QDialog):
@@ -239,29 +239,57 @@ def mouseReleaseEvent(self, e):
239239
class ScoreTable(QtWidgets.QTableWidget):
240240
...
241241

242-
# 可编辑、阻止输入框部分点击事件冒泡
243-
class BetterQCombox(QComboBox):
244-
resize = pyqtSignal()
245-
def __init__(self, parent=None):
246-
super(BetterQCombox, self).__init__(parent)
247-
248-
def mousePressEvent(self, event):
249-
# 拦截鼠标按下事件,阻止事件冒泡
250-
super().mousePressEvent(event)
251-
event.accept()
252-
253-
def mouseReleaseEvent(self, event):
254-
# 拦截鼠标释放事件,阻止事件冒泡
255-
super().mouseReleaseEvent(event)
256-
event.accept()
257-
258-
def resizeEvent(self, e):
259-
self.resize.emit()
260-
261-
262-
263-
264242

243+
class CountryComboBox(QComboBox):
244+
def __init__(self, countries, parent=None):
245+
super().__init__(parent)
246+
247+
# 1. 基础设置
248+
self.setEditable(True)
249+
self.lineEdit().setAlignment(Qt.AlignCenter)
250+
self.setInsertPolicy(QComboBox.NoInsert)
251+
self.view().setTextElideMode(Qt.ElideNone)
252+
self.view().setSpacing(2)
253+
self.view().setLayoutDirection(Qt.LeftToRight)
254+
255+
# 2. 设置 model
256+
self._model = QStringListModel(countries)
257+
self._proxy_model = QSortFilterProxyModel(self)
258+
self._proxy_model.setSourceModel(self._model)
259+
self._proxy_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
260+
self._proxy_model.setFilterKeyColumn(0)
261+
262+
self.setModel(self._model)
263+
264+
# 3. 设置补全器
265+
self._completer = QCompleter(self._proxy_model, self)
266+
self._completer.setCompletionMode(QCompleter.PopupCompletion)
267+
self._completer.setFilterMode(Qt.MatchContains) # 包含搜索
268+
self._completer.popup().setTextElideMode(Qt.ElideNone)
269+
self._completer.popup().setLayoutDirection(Qt.LeftToRight)
270+
self._completer.popup().setStyleSheet("QListView { text-align: center; color: #3d3d3d; font: 12pt '微软雅黑';}")
271+
self.setCompleter(self._completer)
272+
273+
# 4. 居中补全框中的文字
274+
self._completer.popup().setUniformItemSizes(True)
275+
self._completer.popup().setWordWrap(False)
276+
277+
# 5. 信号绑定:输入时过滤
278+
self.lineEdit().textEdited.connect(self._on_text_edited)
279+
280+
# 6. 居中下拉框中的文字
281+
self.setStyleSheet("""
282+
QComboBox QAbstractItemView {
283+
text-align: center;
284+
}
285+
QComboBox {
286+
qproperty-alignment: 'AlignCenter';
287+
}
288+
""")
289+
290+
def _on_text_edited(self, text):
291+
self._proxy_model.setFilterFixedString(text)
292+
self._completer.complete() # 打开补全框
265293

266294

267295

0 commit comments

Comments
 (0)