【问题标题】:Pyqt 5 how to make QLineEdit clickablePyqt 5 如何使 QLineEdit 可点击
【发布时间】:2016-01-27 20:25:57
【问题描述】:

我只是想知道如何使QLineEdit 可点击,因为我想在点击QLineEdit 时清除该行的文本。

【问题讨论】:

标签: python-3.x pyqt5


【解决方案1】:

这是我的 2 美分...

定义:

    from PyQt5.QtWidgets import QLineEdit
    from PyQt5.QtCore import pyqtSlot
    from PyQt5.QtCore import pyqtSignal

    class cQLineEdit(QLineEdit):
        clicked= pyqtSignal()
        def __init__(self,widget):
            super().__init__(widget)
        def mousePressEvent(self,QMouseEvent):
            self.clicked.emit()

用法:

    self.cLE = cQLineEdit(self)
    self.cLE.setFixedWidth(20)
    self.cLE.move(10,200)
    self.cLE.clicked.connect(self.printText)

def printText(self):
    print("Yop,+++")

希望这能有所帮助。

【讨论】:

    【解决方案2】:

    尝试使用下面的代码使QLineEdit 可点击:

    class ClickableLabel(QLabel):
    
        clicked = pyqtSignal()
        def __init__(self,name, widget):
            super().__init__(name, widget)
        def mousePressEvent(self, QMouseEvent):
            self.clicked.emit()
    

    【讨论】:

    • 代码中写的是QLabel,而不是QLineEdit
    猜你喜欢
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2014-02-07
    相关资源
    最近更新 更多