【问题标题】:Pyside - Select all text when QLineEdit gets focusPyside - 当 QLineEdit 获得焦点时选择所有文本
【发布时间】:2014-05-29 09:26:08
【问题描述】:

我是 Qt/PySide 的新手。我希望QLineEdit 在获得焦点时选择其中的所有文本。获得焦点并选择所有文本后,只有在失去焦点并再次获得焦点后才能选择所有文本。当我在QLineEdit 获得焦点后更改光标位置时,它不应该选择所有文本。我该怎么做?

更新:我当前的代码按照 Ashwani Kumar 的建议进行了改进。我仍然无法让它工作:

import sys
from PySide.QtGui import QLineEdit, QApplication, QVBoxLayout, QWidget
class MyLineEdit(QLineEdit):
    def __init__(self, parent=None):
        super(MyLineEdit, self).__init__(parent)

    def focusInEvent(self, e):
        self.selectAll()      

app = QApplication(sys.argv)
top = QWidget()
layout = QVBoxLayout()
layout.addWidget(MyLineEdit())
layout.addWidget(MyLineEdit())
top.setLayout(layout)
top.show()
app.exec_()

【问题讨论】:

    标签: python qt pyside qlineedit


    【解决方案1】:

    您必须继承QLineEdit,然后使用新类而不是QLineEdit。 例如:-

    class MyLineEdit(QtGui.QLineEdit):
        def __init__(self, parent=None)
            super(MyLineEdit, self).__init__(parent)
    
        def focusInEvent(self, e):
            self.selectAll()
    
    
    lineedit = MyLineEdit()
    

    【讨论】:

    • 方法名有错别字。它必须是“focusInEvent”
    • 听从了您的建议。仍然无法弄清楚出了什么问题。
    • 代码在我这边写得很好。你面临什么问题。
    • 文本没有被选中。
    • 如果我使用其他命令,例如 self.undo() 代替 self.selectAll() 它可以工作。但由于某种原因,self.selectAll() 不起作用。
    【解决方案2】:

    使用focusInEvent,当您单击小部件时,它会被执行,但由于您单击,它会删除选定的文本。

    要克服这个问题,我们必须使用mousePressEvent,这可以通过两种方式完成:

    import sys
    from PySide.QtGui import QLineEdit, QApplication, QVBoxLayout, QWidget
    class MyLineEdit(QLineEdit):
        def __init__(self, parent=None):
            super(MyLineEdit, self).__init__(parent)
    
        def mousePressEvent(self, e):
            self.selectAll()      
    
    app = QApplication(sys.argv)
    top = QWidget()
    layout = QVBoxLayout()
    layout.addWidget(MyLineEdit())
    layout.addWidget(MyLineEdit())
    top.setLayout(layout)
    top.show()
    app.exec_()
    

    或者您可以通过简单地覆盖基类 QLineEdit 来做到这一点:

    txt_demo = QtGui.QLineEdit()
    txt_demo.mousePressEvent = lambda _ : txt_demo.selectAll()
    

    但是,由于我们正在修改mousePressEvent,所以每当您尝试单击文本时,它总是会先全选。

    【讨论】:

    • 谢谢。我也使用了局部变量和扩展的 focusOut 来确保在第二次点击时文本将被取消选择并且光标将定位在点击的地方。
    • 太棒了,很高兴我能帮上忙!
    【解决方案3】:

    对于未来的访问者,我正在发布对我有用的代码。由于我是新手,我不确定代码是否包含任何不当行为。如果确实可以发表评论,我会更新我的代码/答案。代码:

    import sys
    from PySide.QtGui import QLineEdit, QApplication, QVBoxLayout, QWidget
    
    class LineEdit(QLineEdit):
        def __init__(self, parent=None):
            super(LineEdit, self).__init__(parent)
            self.readyToEdit = True
    
        def mousePressEvent(self, e, Parent=None):
            super(LineEdit, self).mousePressEvent(e) #required to deselect on 2e click
            if self.readyToEdit:
                self.selectAll()
                self.readyToEdit = False
    
        def focusOutEvent(self, e):
            super(LineEdit, self).focusOutEvent(e) #required to remove cursor on focusOut
            self.deselect()
            self.readyToEdit = True
    
    app = QApplication(sys.argv)
    top = QWidget()
    layout = QVBoxLayout()
    layout.addWidget(LineEdit())
    layout.addWidget(LineEdit())
    top.setLayout(layout)
    top.show()
    app.exec_()
    

    【讨论】:

    • self.deselect() 调用似乎是多余的,但我在 QGraphicsView 中使用它,因此结果可能会有所不同。
    • 确实似乎没有必要。失焦时,即使没有该行,文本也会自动取消选择。
    【解决方案4】:

    QTimerQtCentre 上的解决方案:

    import types
    from PyQt4 import QtCore
    
    def bind(func, to):
        "Bind function to instance, unbind if needed"
        return types.MethodType(func.__func__ if hasattr(func, "__self__") else func, to)
    
    ...
    self.txtSrc.focusInEvent = bind(lambda w, e: QtCore.QTimer.singleShot(0, w.selectAll), self.txtSrc)
    

    此外,提供的解决方案不需要子类化QLineEdit

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2012-12-02
      • 1970-01-01
      • 2010-10-03
      • 2010-12-31
      • 2023-04-04
      • 2011-02-08
      相关资源
      最近更新 更多