QLineEdit readOnly 也禁用清除按钮

QLineEdit readOnly disables clear button as well

基本上这个 post 描述了问题,但是答案是针对 c++ 的,我想知道 python.

中是否有办法做到这一点

将 lineedit 转换为只读模式也会禁用 clearButton,我想知道是否可以保持 clearButton 功能处于活动状态。 是否可以使用某些自定义功能扩展在单击 clearButton 时触发的操作(如果完全可以访问 clearbutton)?

readOnly is set to QLineEdit it disables QToolButton (clear button), so the solution is to enable it, and for this you must obtain the button using the findChild()方法:

from PySide2 import QtWidgets


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    le = QtWidgets.QLineEdit(
        text="Stack Overflow", readOnly=True, clearButtonEnabled=True
    )
    le.findChild(QtWidgets.QToolButton).setEnabled(True)
    le.show()
    sys.exit(app.exec_())