Clear QLineEdit on click event

前端 未结 3 1476
暗喜
暗喜 2020-11-28 16:07

I am using the given code, I want the user to enter text in the QLineEdit widget, press the Copy! button and see the inputted text replace the \'N/A\' label. My questions is

相关标签:
3条回答
  • 2020-11-28 16:43

    I have an optional solution in one line:

    self.lineEdit.mouseReleaseEvent = self.copy_and_print
    

    Make sure you are receiving two parameters in your function (self,event) I hope I helped you

    Full post: https://wiki.python.org/moin/PyQt/Making%20non-clickable%20widgets%20clickable

    0 讨论(0)
  • 2020-11-28 16:45

    The solution is to promote QtDesigner use our custom QLineEdit where we implement the signal clicked with the help of mousePressEvent, this class will be called ClickableLineEdit and the file will be called ClickableLineEdit.py.

    ClickableLineEdit.py

    from PyQt5.QtCore import pyqtSignal
    from PyQt5.QtWidgets import QLineEdit
    
    
    class ClickableLineEdit(QLineEdit):
        clicked = pyqtSignal()
        def mousePressEvent(self, event):
            self.clicked.emit()
            QLineEdit.mousePressEvent(self, event)
    

    To promote it, the following structure will be considered:

    .
    ├── ClickableLineEdit.py
    ├── main.py  
    ├── your.ui
    └── QLineEdit_test.py
    

    Open the design with Qt Designer and right click on the QLineEdit and select Promote to ...:

    A menu will open and place the following

    then press and Promote. Then we generate the code again.

    Then we connect the signal to clear:

    class MainWindow(QMainWindow, QLineEdit_test.Ui_QLineEdit_test):
    
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.setupUi(self)
    
            self.copy_button.clicked.connect(self.copy_and_print)
            self.lineEdit.clicked.connect(self.lineEdit.clear)
    
        def copy_and_print(self):
            self.label.setText(self.lineEdit.text())
    

    Update:

    PySide2:

    from PySide2 import QtCore, QtWidgets
    
    
    class ClickableLineEdit(QtWidgets.QLineEdit):
        clicked = QtCore.Signal()
    
        def mousePressEvent(self, event):
            super(ClickableLineEdit, self).mousePressEvent(event)
            self.clicked.emit()
    
    
    class App(QtWidgets.QWidget):
        def __init__(self):
            super().__init__()
    
            self.lineedit = ClickableLineEdit()
            self.lineedit.clicked.connect(self.lineedit.clear)
    
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(self.lineedit)
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication.instance()
        if app is None:
            app = QtWidgets.QApplication(sys.argv)
        ex = App()
        ex.show()
        sys.exit(app.exec_())
    
    0 讨论(0)
  • 2020-11-28 16:45
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
    
        layout = QGridLayout()
        self.setLayout(layout)
    
        self.lineedit = QLineEdit()
        self.lineedit.returnPressed.connect(self.press)
        layout.addWidget(self.lineedit, 0, 0)
    
    def press(self):
        print("Hi World")
        self.lineedit.clear()
    
    0 讨论(0)
提交回复
热议问题