单击事件时清除 QLineEdit [英] Clear QLineEdit on click event

查看:104
本文介绍了单击事件时清除 QLineEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用给定的代码,我希望用户在 QLineEdit 小部件中输入文本,按复制!按钮并查看输入的文本替换N/A"标签.我的问题是:按照此过程,如何通过简单的鼠标单击清除 QLineEdit 小部件中输入的文本?

从我读到的(

将打开一个菜单并放置以下内容

然后按并推广.然后我们再次生成代码.

然后我们连接信号清除:

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

更新:

PySide2:

from PySide2 import QtCore, QtWidgets类 ClickableLineEdit(QtWidgets.QLineEdit):单击 = QtCore.Signal()def mousePressEvent(self, event):super(ClickableLineEdit, self).mousePressEvent(event)self.clicked.emit()类应用程序(QtWidgets.QWidget):def __init__(self):super().__init__()self.lineedit = ClickableLineEdit()self.lineedit.clicked.connect(self.lineedit.clear)躺 = QtWidgets.QVBoxLayout(self)lay.addWidget(self.lineedit)如果 __name__ == "__main__":导入系统app = QtWidgets.QApplication.instance()如果应用程序是无:app = QtWidgets.QApplication(sys.argv)前 = 应用程序()例如.show()sys.exit(app.exec_())

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: following this procedure, how can I clear the text inputted in the QLineEdit widget with a simple mouse click?

From what I read (this, this and this) it seems like I need to reimplement focusInEvent() in a new class extending QLineEdit. My problem is that the code for my GUI has been imported from Qt Designer using pyuic5 and the examples cited above don't seem to take this in consideration.

Here is my code:

from PyQt5.QtWidgets import *
import sys

import QLineEdit_test


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)

    def copy_and_print(self):

        self.label.setText(self.lineEdit.text())


def main():

    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    app.exec_()

if __name__ == "__main__":
    main()

Here is my converted .ui file:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_QLineEdit_test(object):
    def setupUi(self, QLineEdit_test):
        QLineEdit_test.setObjectName("QLineEdit_test")
        QLineEdit_test.resize(300, 200)
        QLineEdit_test.setMaximumSize(QtCore.QSize(300, 200))
        self.centralwidget = QtWidgets.QWidget(QLineEdit_test)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setMaximumSize(QtCore.QSize(120, 16777215))
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 0, 1, 1)
        self.copy_button = QtWidgets.QPushButton(self.centralwidget)
        self.copy_button.setObjectName("copy_button")
        self.gridLayout.addWidget(self.copy_button, 1, 0, 1, 1)
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setMaximumSize(QtCore.QSize(200, 20))
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 2, 0, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        QLineEdit_test.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(QLineEdit_test)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 300, 22))
        self.menubar.setObjectName("menubar")
        QLineEdit_test.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(QLineEdit_test)
        self.statusbar.setObjectName("statusbar")
        QLineEdit_test.setStatusBar(self.statusbar)

        self.retranslateUi(QLineEdit_test)
        QtCore.QMetaObject.connectSlotsByName(QLineEdit_test)

    def retranslateUi(self, QLineEdit_test):
        _translate = QtCore.QCoreApplication.translate
        QLineEdit_test.setWindowTitle(_translate("QLineEdit_test", "MainWindow"))
        self.copy_button.setText(_translate("QLineEdit_test", "Copy!"))
        self.copy_button.setShortcut(_translate("QLineEdit_test", "Return"))
        self.label.setText(_translate("QLineEdit_test", "N/A"))

解决方案

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

这篇关于单击事件时清除 QLineEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆