且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 QComboBox 中标记当前项目文本?

更新时间:2023-01-28 15:10:31

你可以通过捕捉回车/回车键自动选择行编辑的文本:

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()