QLineEdit
只能输入纯文本,不能输入富文本
构造函数
QLineEdit(self):仅指定依附的父容器QLineEdit(text, self):还指定初始文本
方法
le = QLineEdit(self)
# 设置文本
le.setText('代码输入的信息')
# 获取真实文本
print(le.text())
# 获取显示文本
print(le.displayText())
# 插入文本
le.insert('插入的文本')
# 设置占位提示文本
le.setPlaceholderText('请输入密码:')
# 设置clear按钮
le.setClearButtonEnabled(True)
# 获取是否激活clear按钮
print(le.isClearButtonEnabled())
# 设置文本输出模式
'''Normal = 0 # 默认,正常
NoEcho = 1 # 不显示,但能获取真实文本,text和displayText区别显现
Password = 2 # 显示密码圆圈
PasswordEchoOnEdit = 3 # 输入时正常,一旦结束输入就是Password'''
le.setEchoMode(QLineEdit.NoEcho)
# 获取输出模式
print(le.echoMode())
# 设置自动提示: 用户输入'北京'会出现以下提示
le.setCompleter(QCompleter(['北京大学', '北京南站', '北京交通大学'], le))
# 内容长度限制
le.setMaxLength(10)
# 获取长度限制
print(le.maxLength())
# 只读模式
le.setReadOnly(True)
# 设置编辑状态
le.setModified(False) # 默认为True,即一直可修改
# 获取编辑状态
print(le.isModified())
光标控制
le = QLineEdit('12333313', self)
# 设置光标
le.setFocus()
# 移动光标
'''参数:
第0个: 参数默认False(不选中),True可以选中经过的文本
第1个: 移动的距离'''
le.cursorBackward(True, 1) # 光标右移
le.cursorForward(True, 1) # 光标左移
# 光标移动一个单词,按空格区分相邻单词。只需传入bool参数,不需要传入移动的长度
le.cursorWordBackward(True) # 右移
le.cursorWordForward(True) # 左移
# 光标移动到特殊位置
le.home(False) # 光标移动到行首
le.end(False) # 光标移动到行尾
le.setCursorPosition(1) # 光标移动到指定索引。如果传入小数,向下取整
# 获取光标
print(le.cursorPosition()) # 光标位置的索引,从0开始
print(le.cursorPositionAt(QPoint(25, 0))) # 坐标下光标指向的字符数量(从左向右计算)
文本边距和方位设置
le = QLineEdit(self)
# le.setContentsMargins(100,0,0,0) #不用这个,这个会把整个控件尺寸改变
# 设置文本距离原点 右下左上 的距离
le.setTextMargins(0, 0, 0, 0)
# 设置文本方位位置
le.setAlignment(Qt.AlignLeft | Qt.AlignBottom) # 左下方

掩码
le = QLineEdit(self)
# 需求:共4位,格式为'AA-00',左边必须为两位大写字母,右边是两位数字。
# 中间用'-'分隔。若无输入,用'*'补全
# 但如果输入数字,输入不上
le.setInputMask('>AA-99;*') # 设置了大写字母,如果输入小写的,自动转换为大写

明文与密文
le = QLineEdit(self)
le.setEchoMode(QLineEdit.Password)
# 添加行为
action = QAction(le)
action.setIcon(QIcon('./resources/不可见.png'))
def func():
if le.echoMode() == QLineEdit.Password:
le.setEchoMode(QLineEdit.Normal)
action.setIcon(QIcon('./resources/可见.png'))
else:
le.setEchoMode(QLineEdit.Password)
action.setIcon(QIcon('./resources/不可见.png'))
action.triggered.connect(func)
# 将行为添加到le
'''参数1: 图标再le中的位置
QLineEdit.LeadingPosition: 头位置
action,QLineEdit.TrailingPosition: 末尾'''
le.addAction(action,QLineEdit.TrailingPosition)

常用编辑功能
le = QLineEdit(self)
# 文本拖拽
le.setDragEnabled(True) # 设置文本内容可拖拽到其他地方
# 退格粘贴截切
le.backspace() # 退格,删除光标左侧的一个字符或者选中的全部字符。
# 注意,若要删除选中的文本,应该用代码le.cursorBackward(True,3)选中而不能手动选中
le.del_() # 删除光标右侧一个字符或者选中的文本,类似于上一个方法
le.clear() # 清空所有文本,等效于setText('')
le.copy() # 复制拷贝
le.cut() # 剪切
le.paste() # 粘贴
le.undo() # 撤销
le.redo() # 重做(反撤销)
# 获取是否能重做撤销
print(le.isUndoAvailable()) # 获取能否撤销
print(le.isRedoAvailable()) # 获取能否重做
# 设置选中
le.setSelection(1, 3) # 选择某些文本,索引从0开始,包含最后一个
le.selectAll() # 全选
le.deselect() # 取消选中
# 获取选中状态
print(le.hasSelectedText()) # 获取当前是否选中了某文本
print(le.selectedText()) # 获取选中的文本内容
print(le.selectionStart()) # 获取选取的初始索引
print(le.selectionEnd()) # 获取选取的末尾索引
print(le.selectionLength()) # 获取选取的长度
系统验证器
class MyQIntValidator(QIntValidator):
def fixup(self, p_str):
'''如果未输入或输入的数字小于18,一旦脱标设置为18'''
print(p_str)
if len(p_str) == 0 or int(p_str) < 18: # 顺序不要颠倒!
return '18'
# 系统这个验证器不能实现修补功能,要修补就要重写类的fixup()方法
# validator = QIntValidator(18, 150)
validator = MyQIntValidator(18, 150)
le = QLineEdit(self)
le.setValidator(validator)
# le_1: 脱标时的测试组件
输入10:
自定义验证器
class AgeVadidator(QValidator):
def validate(self, input_str, pos_int):
'''重写这个方法'''
print(input_str, pos_int)
try:
if 18 <= int(input_str) <= 150:
return (QValidator.Acceptable, input_str, pos_int)
elif 1 <= int(input_str) <= 15:
return (QValidator.Intermediate, input_str, pos_int)
else:
return (QValidator.Invalid, input_str, pos_int)
except:
if len(input_str) == 0:
return (QValidator.Intermediate, input_str, pos_int)
return (QValidator.Invalid, input_str, pos_int)
def fixup(self, p_str):
'''重写这个方法'''
print("xxxx", p_str)
try:
if int(p_str) < 18:
return '18'
return '180'
except:
return '18'
validator = AgeVadidator()
le = QLineEdit(self)
le.setValidator(validator)

信号
le = QLineEdit(self)
le.textEdited.connect(lambda text: print('文本正在被编辑,内容为:', text))
le.textChanged.connect(lambda text: print('文本框文本发生了改变', text))
le.returnPressed.connect(lambda: print('回车键被按下了'))
le.editingFinished.connect(lambda: print('文本编辑结束了'))
le.selectionChanged.connect(lambda: print('被选中的文本发生了改变'))
le.cursorPositionChanged.connect(lambda old_pos, new_pos: print('光标位置发生改变,位置为{},新位置为{}'.format(old_pos, new_pos)))
QTextEdit
简单方法
te = QTextEdit('默认内容', self)
# 设置占位提示文本
te.setPlaceholderText(text)
# 获取占位提示文本
print(te.placeholderText())
# 纯文本
te.setPlainText(text) # 设置纯文本内容,光标会移动到文本最前边
te.insertPlainText(text) # 插入
te.toPlainText() # 获取纯文本内容
# 富文本
te.setHtml(html) # 设置html
te.insertHtml(html) # 插入html
te.toHtml() # 获取html内容
# 自动判定
te.setText(content) # 自动判定是html还是纯文本并设置
te.append(content) # 自动判定并在末尾添加
# 文本权限
te.setReadOnly(True) # 设置为只读
te.isReadOnly() # 获取是否为只读
# 只读状态下代码可插入文本:
te.insertPlainText(text)
# 文本操作
te.clear() # 清空,等效于setText('')
print(te.document()) # 获取文档对象
print(te.textCursor()) # 获取光标对象
# 段落对齐方式(只作用于当前段落)
"""QT.AlignLeft #靠左对齐(默认)
Qt.AlignRight #靠右对齐
Qt.AlignCenter #居中对齐"""
te.setAlignment(Qt.AlignCenter)
# 前后景色
te.setTextBackgroundColor(QColor(255, 0, 0)) # 设置背景颜色
te.setTextColor(QColor(0, 255, 0)) # 设置字体颜色(前景颜色)
# 让光标移动到当前te
te.setFocus()
滚动条
te = QTextEdit('哈哈哈', self)
# 滚动条显示策略
te.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) # 竖直滚动条一直显示
te.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn) # 水平滚动条一直显示
# 设置边角(竖直和水平滚动条的右下交点)按钮
cor_btn = QPushButton(self)
cor_btn.setIcon(QIcon('./resources/unity.png'))
cor_btn.clicked.connect(lambda: print('变焦按钮被点击了'))
te.setCornerWidget(cor_btn) # 将按钮添加到QTextEdit控件

字体格式
btn_refer = QPushButton('字体选择框', self)
btn_refer.move(60, 250)
# 打开字体选择对话框
btn_refer.clicked.connect(lambda: QFontDialog.getFont())
# 使用QTextEdit下的字体设置
def func():
te.setFontFamily('草书') # 设置在字体类型
te.setFontWeight(QFont.Black) # 加粗级别。Black是最粗
te.setFontPointSize(12) # 设置字体大小
te.setFontItalic(True) # 加粗
te.setFontUnderline(True) # 添加下划线
te.setFocus()
btn.clicked.connect(func)
############################
# 使用QFont的字体设置
font = QFont()
font.setStrikeOut(True) # 添加删除线(中划线),删除线在te下不提供,只能这样设置
te.setCurrentFont(font)


字符设置
# 字符设置
# 使用QTextCharFormat的字体设置
tcf = QTextCharFormat()
tcf.setFontFamily('宋体') # 设置字体类型
tcf.setFontPointSize(12) # 设置字体大小
tcf.setFontCapitalization(QFont.Capitalize) # 每个单词首字母大写
tcf.setForeground(QColor(100, 200, 0)) # 设置前景色(字体色)
te.setCurrentCharFormat(tcf)
# 若有两个格式,覆盖还是合并?
tcf_1 = QTextCharFormat()
tcf_1.setFontStrikeOut(True) # 删除线
tcf_1.setFontUnderline(True) # 下划线
tcf_1.setUnderlineColor(QColor(255, 0, 0)) # 设置下划线的颜色
# te.setCurrentCharFormat(tcf_1) # 覆盖tcf
te.mergeCurrentCharFormat(tcf_1) # 合并两个样式
换行策略
# 设置无软换行
te.setLineWrapMode(QTextEdit.NoWrap) # 字数超过了宽度会显示水平滚动条
# 设置有软换行:宽度限制为像素还是列数取决于这个枚举值
te.setLineWrapColumnOrWidth(80) # 宽度最大为80像素
te.setLineWrapMode(QTextEdit.FixedPixelWidth)
te.setLineWrapColumnOrWidth(5) # 宽度最大为5列
te.setLineWrapMode(QTextEdit.FixedColumnWidth)
# 获取换行策略
print(te.lineWrapMode())
# 单词完整性策略
te.setWordWrapMode(QTextOption.WordWrap) # 保持单词完整
移动到锚点
# 点击按钮,滚动到锚点
te.insertHtml('a' * 700 + '<a name="lzc" href="#anchor">林在超</a>' + 'b' * 600)
btn.clicked.connect(lambda: te.scrollToAnchor('anchor'))
Tab键位控制
# 有关Tab键的设置
te.setTabChangesFocus(True) # 设置tab的作用仅为改变光标
te.setTabStopDistance(100) # 设置tab的间距为100像素
# 获取信息
print(te.tabChangesFocus()) # 获取是否为仅为改变光标状态
print(te.tabStopDistance()) # float型的一个tab的宽度,默认80.0
print(te.tabStopWidth()) # int型的一个tab的宽度,默认80
覆盖模式
te = QTextEdit(self)
te.setOverwriteMode(True) # 设置覆盖模式
print(te.overwriteMode()) # 获取覆盖模式
# 点击按钮,执行如下逻辑
if not te.overwriteMode(): # 如果没有开启覆盖模式
# 开启覆盖模式
te.setOverwriteMode(True) # 输入字符会把光标后的字符替换掉
te.setCursorWidth(1) # 设置光标宽度为1
else:
te.setOverwriteMode(False)
te.setCursorWidth(10) # 设置光标宽度为10

光标控制
te = QTextEdit(self)
# 获取光标对象
tc = te.textCursor()
# 设置光标
'''参数:
参数0:光标停在的位置
参数2:可选,默认为锚点随光标移动,可设置不移动,即为选中内容'''
tc.setPosition(6, QTextCursor.KeepAnchor)
# 移动光标
tc.movePosition(QTextCursor.Up, QTextCursor.KeepAnchor, 1) # 向上一行选中文本
tc.select(QTextCursor.WordUnderCursor) # 直接选择
# 可设置参数来设置不同选择方式:这里是选中光标所在的单词
te.setTextCursor(tc) # 最终要写这句才能设置光标
te.setFocus()
# 获取光标位置
print('是否在段落开始?', tc.atBlockStart())
print('是否在段落末尾?', tc.atBlockEnd())
print('是否在整个文档开始?', tc.atStart())
print('是否在整个文档?', tc.atEnd())
print('在第{}列'.format(tc.columnNumber()))
print('在全局的索引位置:', tc.position())
print('在文本块的索引位置:', tc.positionInBlock())
选中内容操作
te = QTextEdit(self)
tc = te.textCursor()
# 获取选中操作
print(tc.selectedText()) # 获取选中的内容_0
print(tc.selection().toPlainText()) # 获取选中内容_1
print(tc.selectedTableCells()) # 获取表格选中信息,返回一个元组。
# 如(0,2,3,2)表示从0行3列开始,选取了2行2列内容
# 选中操作
tc.clearSelection() # 取消选中
te.setTextCursor(tc) # 要设置反向操作
tc.removeSelectedText() # 删除选中的内容
print(tc.selectionStart()) # 获取选中的开始索引
print(tc.selectionEnd()) # 获取选中的结束索引
print(tc.hasSelection()) # 判断是否有选中的文本
# 删除操作
tc.deleteChar() # 删除光标右侧一个字符或选中的所有内容
tc.deletePreviousChar() # 删除光标左侧一个字符或选中的所有内容
打开超链接
class MyQTextEdit(QTextEdit):
'''必须重写这个类'''
def mousePressEvent(self, event):
print(event.pos()) # 获取鼠标坐标
url = self.anchorAt(event.pos()) # 获取描点的url
if url:
QDesktopServices.openUrl(QUrl(url))
return super().mousePressEvent(event)
te = MyQTextEdit(self)
te.resize(200, 200)
te.move(10, 10)
te.insertHtml('<a href="http://www.bjtu.edu.cn">超链接<>/a')
常用编辑操作
te = QTextEdit(self)
# 菜单编辑
te.copy() # 复制
te.paste() # 粘贴
te.selectAll() # 全选
te.find('xxx', QTextDocument.FindBackward | QTextDocument.FindCaseSensitively | QTextDocument.FindWholeWords)
# 查找:第1个参数可选。默认向前(向右)、忽略大小写、不必满足整个单词。但可以加参数设置。若查找到就会选择并返回True
# 光标编辑
tc = te.textCursor()
tc.insertText('aa')
tc.insertBlock()
tc.beginEditBlock() # 开始编辑
tc.insertText('bb')
tc.insertBlock()
tc.insertText('cc')
tc.insertBlock()
tc.endEditBlock() # 结束编辑,直接绑定4步,以后undo或redo可以直接跳跃这些步数
te.setFocus()
插入内容
# 插入文本
tcf = QTextCharFormat()
tcf.setToolTip('提示文本') # 设置提示文本
tcf.setFontFamily('楷书') # 设置字体
tcf.setFontPointSize(24) # 设置字号
tc = te.textCursor()
tc.insertText('这是插入内容', tcf)
tc.insertHtml('<a href="http://www.bjtu.edu.cn">超链接</a>')
# 插入列表
tc = te.textCursor()
# tl = tc.insertList(QTextListFormat.ListUpperRoman) # 插入列表,参数列表左侧样式
# tl = tc.createList(QTextListFormat.ListDecimal) # 创建列表,参数为样式
tlf = QTextListFormat()
tlf.setIndent(2) # 设置缩进为一个Tab键
tlf.setNumberPrefix('>>') # 设置列表符左边的装饰
tlf.setNumberSuffix('<<') # 设置列表符右边的装饰
tlf.setStyle(QTextListFormat.ListDecimal) # 必须设置十进制数字才会展示列表符
tl = tc.createList(tlf) # 创建列表_2
# 插入图片
tc = te.textCursor() # 实例化文本光标对象
tif = QTextImageFormat() # 实例化图片插入对象
tif.setName('./resources/unity.png') # 图片路径
tif.setWidth(50) # 图片宽度
tif.setHeight(50) # 图片高度
tc.insertImage(tif, QTextFrameFormat.FloatLeft) # 图片悬浮在左边
# 插入html
tc = te.textCursor()
tdf = QTextDocumentFragment.fromHtml('<h5>插入的html句子 </h5>') # 插入html
# fromPlainText('<h5>插入的超文本句子</h5>') # 插入纯文本
tc.insertFragment(tdf)
# 插入句子
tc = te.textCursor()
ttf = QTextTableFormat()
ttf.setAlignment(Qt.AlignLeft) # 设置对齐方式:左对齐
ttf.setCellPadding(6) # 设置内间距
ttf.setCellSpacing(5) # 设置外间距
# 设置列宽约束,3列的百分比分别为50%,30%,20%
ttf.setColumnWidthConstraints((QTextLength(QTextLength.PercentageLength, 50),
QTextLength(QTextLength.PercentageLength, 30),
QTextLength(QTextLength.PercentageLength, 20)))
table = tc.insertTable(4, 3, ttf) # 插入表格,4行3列,样式为ttf
table.appendColumns(1) # 表格追加一列
# 插入文本块
tbf = QTextBlockFormat()
tbf.setAlignment(Qt.AlignRight) # 设置对齐方式:右对齐
tbf.setRightMargin(100) # 设置文本右侧间距,距离右边框为100像素
tbf.setIndent(2) # 设置文本块距离左边缩进:2个Tab
tcf = QTextCharFormat()
tcf.setFontFamily('隶书') # 字体
tcf.setFontItalic(True) # 字体是否倾斜:True
tcf.setFontPointSize(12) # 设置字号
tc = te.textCursor()
tc.insertBlock(tbf, tcf) # 给文本框可加入两个元素来决定样式
te.setFocus()
# 插入文本框架
tff = QTextFrameFormat() # 实例化文本框架
tff.setBorder(5) # 设置框架的框宽度
tff.setBorderBrush(QColor(200, 100, 100)) # 设置框架的框颜色
tff.setRightMargin(20) # 设置框架和文本框右边框的间距:20像素
doc = te.document() # 实例化文本编辑对象
root_frame = doc.rootFrame() # 设置另一个框架
root_frame.setFrameFormat(tff) # 另一个框架的样式和上面的相同
tc = te.textCursor()
tc.insertFrame(tff)
设置合并和获取格式
te = QTextEdit(self)
# 字符格式
'''需要在两行输入的中键隔开一行,在执行这个函数才会有效果'''
tcf = QTextCharFormat()
tcf.setFontFamily('微软雅黑') # 设置字体类型
tcf.setFontPointSize(12) # 设置字体大小
tcf.setFontOverline(True) # 加上划线
tcf.setFontUnderline(True) # 加下划线
tc = te.textCursor()
tc.setBlockCharFormat(tcf)
# 文本块格式
tbf = QTextBlockFormat() # 实例化文本块格式对象
tbf.setAlignment(Qt.AlignRight)
tbf.setIndent(2)
tc = te.textCursor()
tc.setBlockFormat(tbf)
# 选中字符格式:对选中的文本执行此函参才有效
tcf = QTextCharFormat()
tcf.setFontFamily('微软雅黑')
tcf.setFontPointSize(12)
tcf.setFontOverline(True)
tcf.setFontUnderline(True)
tc = te.textCursor()
tc.setCharFormat(tcf) # 设置字符格式
tcf_1 = QTextCharFormat()
tcf_1.setFontStrikeOut(True) # 加删除线
tc.mergeCharFormat(tcf_1) # 合并字符格式,包含上面的两种格式
# 获取格式
tc = te.textCursor()
print(tc.block().text()) # 获取文本块内容
print(tc.blockNumber()) # 获取文本光标所在的行数
print(tc.currentList().count()) # 获取所含列表的数量,必须添加列表之后才能使用
信号监听
te = QTextEdit(self)
te.textChanged.connect(lambda: print('文本内容改变了'))
te.selectionChanged.connect(lambda: print('所选内容改变了'))
te.copyAvailable.connect(lambda yes: print('当前可复制?', yes))
QPlainTextEdit
简单方法
QPlainText的很多方法在QTextEdit都能找到
qte = QPlainTextEdit(self)
# 文本块的最大数量
qte.setMaximumBlockCount(3) # 设置最大数量,当超出最大,靠前的块完全删除(不是被隐藏)
print(qte.blockCount()) # 获取当前文本块的数量
# 文本操作
qte.setPlainText('设置文本')
qte.insertPlainText('插入文本')
qte.appendPlainText('追加文本')
qte.appendHtml('<a href="#">超链接</a>') # 追加html时,不支持table(表格),列表,图片
print(qte.toPlainText()) # 获取纯文本内容,不会获取到html
# 设置占位提示文本
qte.setPlaceholderText('请输入内容...')
# 获取占位文本的内容
print(qte.placeholderText())
# 设置为只读
qte.setReadOnly(False)
# 判断是否为只读
print(qte.isReadOnly())
# 自动换行
qte.setLineWrapMode(QPlainTextEdit.NoWrap) # 默认为WidgetWidth(软换行),这里是不换行
# 获取换行状态
print(qte.lineWrapMode())
# 覆盖模式
qte.setOverwriteMode(True) # 默认为False(不覆盖),只能向光标后覆盖一个英文字符
# 判断是否为覆盖模式
print(qte.overwriteMode())
# 设置Tab只能转换光标
qte.setTabChangesFocus(False) # 默认为False(仅制表)。如果为True焦点从一个控件移动到另一个控件
qte.setTabStopDistance(120) # 在False下,可设置Tab的像素距离,默认为80
光标操作
qte = QPlainTextEdit(self)
# tc = qte.QTextCursor()
# tc.insertImage('./resources/unity.png')
# tc = qte.cursorForPosition(QPoint(100,50)) #在指定坐标处的光标插入内容
# tc.insertText('插入内容')
# qte.setCursorWidth(12) #设置光标宽度
qte.moveCursor(QTextCursor.End, QTextCursor.KeepAnchor) # 指定光标的末位置,从当前光标到末尾选中内容
常用编辑操作
qte = QPlainTextEdit(self)
# 放缩
qte.zoomIn(10) # 正数放大,负数缩小,默认为1
# 滚动到光标
qte.setCenterOnScroll(True) # 可以在使用centerCursor时让最末尾的光标也显示在中间
# qte.centerCursor() # 光标移动到中间
qte.ensureCursorVisible() # 尽量少移动,只要能看到光标就行
格式设置
qte = QPlainTextEdit(self)
# 格式设置
tcf = QTextCharFormat()
tcf.setFontUnderline(True) # 加下划线
tcf.setUnderlineColor(QColor(255, 5, 0)) # 设置下划线颜色
qte.setCurrentCharFormat(tcf)
信号
qte.textChanged.connect(lambda: print('文本内容改变了'))
qte.selectionChanged.connect(lambda: print('选中内容发生了改变', qte.textCursor().selectedText()))
qte.modificationChanged.connect(lambda var: print('修改状态发生了改变', var))
qte.cursorPositionChanged.connect(lambda: print('光标位置改变了'))
qte.blockCountChanged.connect(lambda: print('文本块的数量改变了'))
# doc = qte.document()
# doc.setModified(False)
qte.updateRequest.connect(lambda rect, dy: line_label.move(line_label.x(), line_label.y() + dy))
QKeySequenceEdit
绑定快捷键
btn = QPushButton('按钮', self)
btn.move(10, 200)
kse = QKeySequenceEdit(self)
# 设置快捷键
# ks = QKeySequence('Ctrl+Z') #使用字符串
# ks = QKeySequence(QKeySequence.Copy) #使用枚举值
ks = QKeySequence(Qt.CTRL + Qt.Key_Z, Qt.CTRL + Qt.Key_Q) # 使用特定的Qt的枚举
kse.setKeySequence(ks) # 添加到kse对象
kse.clear() # 清空快捷键
btn.clicked.connect(lambda: print(kse.keySequence().toString(), kse.keySequence().count()))
信号
kse = QKeySequenceEdit(self)
kse.editingFinished.connect(lambda: print('结束编辑'))
kse.keySequenceChanged.connect(lambda key: print('键位序列改变了', key.toString()))

QFrame
frame = QFrame(self)
frame.resize(200, 200)
frame.move(50, 50)
frame.setStyleSheet('background-color:cyan;')
# frame.setFrameShape(QFrame.Box) # 设置frame的形状
# frame.setFrameShadow(QFrame.Raised) # 设置阴影的形状
frame.setFrameStyle(QFrame.Box | QFrame.Raised) # 可把阴影和frame形状放这个函数中,与上面两个等效
frame.setLineWidth(6) # 设置外线的宽度
frame.setMidLineWidth(3) # 设置中线的宽度
print(frame.frameWidth()) # 获取总线的宽度 = 外线+阴影+内线(和外线等宽)
frame.setFrameRect(QRect(80, 80, 60, 60)) # 设置矩形的宽度

该博客主要介绍了Python中Qt的多个控件。涵盖QLineEdit、QTextEdit、QPlainTextEdit、QKeySequenceEdit和QFrame等控件,详细阐述了各控件的构造函数、方法、光标控制、编辑功能、信号等方面内容,如QLineEdit只能输入纯文本,QPlainText很多方法与QTextEdit类似。
955

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



