本文介绍了如何在 QComboBox 中标记当前项目文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将组合框用作带有历史记录的简单命令行.
I'm using a combo box as a simple command line with a history.
这是信号槽定义:
QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return),
self.comboBox_cmd,
activated=self.queryLine)
...和插槽:
@QtCore.pyqtSlot()
def queryLine(self):
'''
Read cmd string from widget and write to device.
'''
## comboBox can be enhanced with a history
cmd = self.comboBox_cmd.currentText()
cmds = [self.comboBox_cmd.itemText(i) for i in range(self.comboBox_cmd.count())]
if not cmds or cmds[-1] != cmd:
self.comboBox_cmd.addItem(cmd)
self.query(cmd)
这真的很好用.现在,如何在按 Enter 后标记当前项目的整个文本,以便我可以根据需要替换整行?
This works really well. Now, how can I mark the entire text of the current item after pressing Enter, so that I can replace the whole line if I so wish?
推荐答案
你可以通过捕捉回车/回车键自动选择行编辑的文本:
You can automatically select the text of the line edit by catching the return/enter key press:
class SelectCombo(QtWidgets.QComboBox):
def keyPressEvent(self, event):
# call the base class implementation
super().keyPressEvent(event)
# if return/enter is pressed, select the text afterwards
if event.key() in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
self.lineEdit().selectAll()
这篇关于如何在 QComboBox 中标记当前项目文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!