需求
QTextEdit 有的时候会需要在按 Enter 案件的时候,进行一定的事件操作。
但是QTextEdit 本身没有这样的 接口,所以我们 需要自定义组件。
如下图:
当我们 QTextEdit 点击 Enter 的时候,会执行相应操作

思路步骤
1.主界面 testEdit.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.12.2
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
from myTextEdit import myTextEdit
class Ui_MainWindow(QMainWindow):
def __init__(self ):
super().__init__()
self.setObjectName("self")
self.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(self)

本文档介绍如何在PyQt5中创建一个自定义的QTextEdit组件,以便在用户按下Enter键时捕获该事件并执行特定操作。通过在主界面的testEdit.py文件中替换默认的QTextEdit为自定义的myTextEdit类,实现了这一功能。myTextEdit类继承自QTextEdit,并重写了keyPressEvent方法来检查Enter键是否被按下,如果按下则打印相关信息。注意区分Key_Enter(小键盘Enter)和Key_Return(主键盘回车)的区别。
最低0.47元/天 解锁文章
696

被折叠的 条评论
为什么被折叠?



