【GUI-pyqt5】QLineEdit类

1. 描述

  1. 是一个单行文本编辑器
  2. 允许用户输入和编辑单行纯文本
  3. 自带一组编辑功能(撤销、重做、剪切、粘贴、拖放)

2. 继承

QWidget

3. 功能作用

3.1 控件的创建

QLineEdit(parent: QWidget=None)
QLineEdit(str, parent: QWidget=None)

3.2 文本设置和获取

3.2.1 API

API功能备注
setText(str)
设置内容文本
-
insert(newText)
在光标处插入内容文本
-
text()
获取真实内容文本
-
displayText()
获取用户能看到的内容文本
-

3.2.2 应用场景

通过代码来控制输入文本内容

3.2.3 案例

创建一个窗口,添加两个文本框和一个按钮
要求:点击按钮后,将文本框A中的内容复制到文本框B中

from PyQt5.Qt import *

import sys

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QWidget()
    w.setWindowTitle('QLE使用')
    w.resize(500,500)

    # 创建文本框和一个按钮
    qle1 = QLineEdit(w)
    qle1.move(10, 10)
    qle2 = QLineEdit(w)
    qle2.move(200, 10)
    btn = QPushButton(w)
    btn.move(25, 40)


    # 文本框1设置文本内容
    qle1.setText('xujie')
    btn.setText('copy')

    # 点击按钮,将文本框1的内容复制给文本框2
    btn.clicked.connect(lambda x: qle2.setText(qle1.text()))


    w.show()
    sys.exit(app.exec_())

在这里插入图片描述

3.3 输出模式

3.3.1 API

API功能备注
setEchoMode(QLineEdit.EchoMode)
设置文本框输出模式
①QLineEdit.NoEcho=0:不输出
②QLineEdit.NormalEcho=1:正常输出
③QLineEdit.Password=2:密文形式
④QLineEdit.PasswordEchoOnEdit=3:编辑时明文,结束后密文
echoMode()
获取文本框输出模式
-

3.3.2 应用场景

设置输出模式,来适应不同的应用场景

3.3.3 案例

模拟用户登录案例
创建一个窗口,添加两个文本框和一个按钮
要求

  1. 一个用作账号
  2. 另一个用作密码
  3. 点击登录后,获取账号和密码信息
  4. 进行比对账号密码信息
    1. 正确账号:xujie
    2. 正确密码:123456
    3. 如果账号错误,则清空账号和密码
    4. 如果仅密码错误,则仅清空密码框
from PyQt5.Qt import *

import sys

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """控件调用方法"""
        # 用户名输入框
        self.label1 = QLabel(self)
        self.edit_1 = QLineEdit(self)
        self.label1.setText('用户名')
        self.label1.move(10, 13)
        self.edit_1.move(70, 10)

        # 密码输入框
        self.label2 = QLabel(self)
        self.edit_2 = QLineEdit(self)
        self.label2.setText('密码')
        self.label2.move(10, 53)
        self.edit_2.move(70, 50)
        self.edit_2.setEchoMode(QLineEdit.Password)

        # 按钮控件
        self.button = QPushButton(self)
        self.button.setText('点击登录')
        self.button.move(80, 90)

        # 动作信号
        self.button.clicked.connect(self.get_command)

    def get_command(self):
        """按钮点击槽函数"""
        name = self.edit_1.text()
        passwd = self.edit_2.text()
        rel_name = 'xujie'
        rel_passwd = '123456'
        if name == rel_name and str(passwd) == str(rel_passwd):
            print('登录成功!!!')
        elif name != rel_name:
            self.edit_1.clear()
            self.edit_2.clear()
        elif str(passwd) != str(rel_passwd):
            self.edit_2.clear()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.setWindowTitle('QLineEdit')
    window.setWindowIcon(QIcon('cats1.ico'))
    window.resize(250, 150)
    window.show()
    sys.exit(app.exec_())

在这里插入图片描述

3.4 占位提示字符串

3.4.1 API

API功能备注
setPlaceholderText(notice_str)
设置占位提示字符串
-
placeholderText()
获取占位提示字符串
-

3.4.2 应用场景

  1. 在用户输入文本内容之前,给用户提示语句
  2. 文本框内容:空:显示提示文本;不空:隐藏提示文本

3.4.3 案例

完善3.3案例,增加占位提示文本

from PyQt5.Qt import *

import sys

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """控件调用方法"""
        # 用户名输入框
        self.label1 = QLabel(self)
        self.edit_1 = QLineEdit(self)
        self.label1.setText('用户名')
        self.label1.move(10, 13)
        self.edit_1.move(70, 10)
        self.edit_1.setPlaceholderText('请输入用户名...')

        # 密码输入框
        self.label2 = QLabel(self)
        self.edit_2 = QLineEdit(self)
        self.label2.setText('密码')
        self.label2.move(10, 53)
        self.edit_2.move(70, 50)
        self.edit_2.setEchoMode(QLineEdit.Password)
        self.edit_2.setPlaceholderText('请输入密码...')

        # 按钮控件
        self.button = QPushButton(self)
        self.button.setText('点击登录')
        self.button.move(80, 90)

        # 动作信号
        self.button.clicked.connect(self.get_command)

    def get_command(self):
        """按钮点击槽函数"""
        name = self.edit_1.text()
        passwd = self.edit_2.text()
        rel_name = 'xujie'
        rel_passwd = '123456'
        if name == rel_name and str(passwd) == str(rel_passwd):
            print('登录成功!!!')
        elif name != rel_name:
            self.edit_1.clear()
            self.edit_2.clear()
        elif str(passwd) != str(rel_passwd):
            self.edit_2.clear()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.setWindowTitle('QLineEdit')
    window.setWindowIcon(QIcon('cats1.ico'))
    window.resize(250, 150)
    window.show()
    sys.exit(app.exec_())

在这里插入图片描述

3.5 清空按钮提示

3.5.1 API

API功能备注
setClearButtonEnabled(bool)
设置清空按钮
-
isClearButtonEnabled()
清空按钮是否开启
-

3.5.2 应用场景

用作快速清空文本框内容

3.5.3 案例

完善3.3.3案例,为密码文本框加上清空按钮

from PyQt5.Qt import *

import sys

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """控件调用方法"""
        # 用户名输入框
        self.label1 = QLabel(self)
        self.edit_1 = QLineEdit(self)
        self.label1.setText('用户名')
        self.label1.move(10, 13)
        self.edit_1.move(70, 10)
        self.edit_1.setPlaceholderText('请输入用户名...')

        # 密码输入框
        self.label2 = QLabel(self)
        self.edit_2 = QLineEdit(self)
        self.label2.setText('密码')
        self.label2.move(10, 53)
        self.edit_2.move(70, 50)
        self.edit_2.setEchoMode(QLineEdit.Password)
        self.edit_2.setPlaceholderText('请输入密码...')
        self.edit_2.setClearButtonEnabled(True)

        # 按钮控件
        self.button = QPushButton(self)
        self.button.setText('点击登录')
        self.button.move(80, 90)

        # 动作信号
        self.button.clicked.connect(self.get_command)

    def get_command(self):
        """按钮点击槽函数"""
        name = self.edit_1.text()
        passwd = self.edit_2.text()
        rel_name = 'xujie'
        rel_passwd = '123456'
        if name == rel_name and str(passwd) == str(rel_passwd):
            print('登录成功!!!')
        elif name != rel_name:
            self.edit_1.clear()
            self.edit_2.clear()
        elif str(passwd) != str(rel_passwd):
            self.edit_2.clear()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.setWindowTitle('QLineEdit')
    window.setWindowIcon(QIcon('cats1.ico'))
    window.resize(250, 150)
    window.show()
    sys.exit(app.exec_())

在这里插入图片描述

3.6 添加操作行为

3.6.1 API

API功能备注
addAction(QAction, QlineEdit.ActionPosition)
添加操作
QlineEdit.ActionPosition:①QlineEdit.LeadingPosition放前面;②QlineEdit.TrailingPosition放后面
addAction(QIcon, QlineEdit.ActionPosition)
添加图标操作
-

3.6.2 应用场景

为文本框添加附加行为操作

3.6.3 案例

完善3.5.3案例,为密码文本框添加明文和密文切换按钮

from PyQt5.Qt import *

import sys

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """控件调用方法"""
        # 用户名输入框
        self.label1 = QLabel(self)
        self.edit_1 = QLineEdit(self)
        self.label1.setText('用户名')
        self.label1.move(10, 13)
        self.edit_1.move(70, 10)
        self.edit_1.resize(200, 24)
        self.edit_1.setPlaceholderText('请输入用户名...')

        # 密码输入框
        self.label2 = QLabel(self)
        self.edit_2 = QLineEdit(self)
        self.label2.setText('密码')
        self.label2.move(10, 53)
        self.edit_2.move(70, 50)
        self.edit_2.resize(200, 24)
        self.edit_2.setEchoMode(QLineEdit.Password)
        self.edit_2.setPlaceholderText('请输入密码...')
        self.edit_2.setClearButtonEnabled(True)

        # 添加密码框明文密文切换按钮,自定义操作
        action = QAction(self.edit_2)
        action.setIcon(QIcon('close_eye.png'))
        def change():
            """改变明文和密文方法"""
            if self.edit_2.echoMode() == QLineEdit.Normal:
                self.edit_2.setEchoMode(QLineEdit.Password)
                action.setIcon(QIcon('close_eye.png'))
            else:
                self.edit_2.setEchoMode(QLineEdit.Normal)
                action.setIcon(QIcon('open_eye.png'))
        action.triggered.connect(change)
        self.edit_2.addAction(action, QLineEdit.TrailingPosition)

        # 按钮控件
        self.button = QPushButton(self)
        self.button.setText('点击登录')
        self.button.move(80, 90)

        # 动作信号
        self.button.clicked.connect(self.get_command)

    def get_command(self):
        """按钮点击槽函数"""
        name = self.edit_1.text()
        passwd = self.edit_2.text()
        rel_name = 'xujie'
        rel_passwd = '123456'
        if name == rel_name and str(passwd) == str(rel_passwd):
            print('登录成功!!!')
        elif name != rel_name:
            self.edit_1.clear()
            self.edit_2.clear()
        elif str(passwd) != str(rel_passwd):
            self.edit_2.clear()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.setWindowTitle('QLineEdit')
    window.setWindowIcon(QIcon('cats1.ico'))
    window.resize(300, 150)
    window.show()
    sys.exit(app.exec_())

在这里插入图片描述

3.7 文本自动补全

3.7.1 API

API功能备注
setCompleter(QCompleter)
设置完成器
-
completer()
查看完成器
-

3.7.2 应用场景

根据用户已经输入的字符串,快速联想补全

3.7.3 案例

完善3.6.3案例,为账户设置文本框,设置补全候选项

from PyQt5.Qt import *

import sys

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """控件调用方法"""
        # 用户名输入框
        self.label1 = QLabel(self)
        self.edit_1 = QLineEdit(self)
        self.label1.setText('用户名')
        self.label1.move(10, 13)
        self.edit_1.move(70, 10)
        self.edit_1.resize(200, 24)
        self.edit_1.setPlaceholderText('请输入用户名...')

        completer = QCompleter(['xujie', 'linfeng', '123456'], self.edit_1)
        self.edit_1.setCompleter(completer)

        # 密码输入框
        self.label2 = QLabel(self)
        self.edit_2 = QLineEdit(self)
        self.label2.setText('密码')
        self.label2.move(10, 53)
        self.edit_2.move(70, 50)
        self.edit_2.resize(200, 24)
        self.edit_2.setEchoMode(QLineEdit.Password)
        self.edit_2.setPlaceholderText('请输入密码...')
        self.edit_2.setClearButtonEnabled(True)

        # 添加密码框明文密文切换按钮,自定义操作
        action = QAction(self.edit_2)
        action.setIcon(QIcon('close_eye.png'))
        def change():
            """改变明文和密文方法"""
            if self.edit_2.echoMode() == QLineEdit.Normal:
                self.edit_2.setEchoMode(QLineEdit.Password)
                action.setIcon(QIcon('close_eye.png'))
            else:
                self.edit_2.setEchoMode(QLineEdit.Normal)
                action.setIcon(QIcon('open_eye.png'))
        action.triggered.connect(change)
        self.edit_2.addAction(action, QLineEdit.TrailingPosition)

        # 按钮控件
        self.button = QPushButton(self)
        self.button.setText('点击登录')
        self.button.move(80, 90)

        # 动作信号
        self.button.clicked.connect(self.get_command)

    def get_command(self):
        """按钮点击槽函数"""
        name = self.edit_1.text()
        passwd = self.edit_2.text()
        rel_name = 'xujie'
        rel_passwd = '123456'
        if name == rel_name and str(passwd) == str(rel_passwd):
            print('登录成功!!!')
        elif name != rel_name:
            self.edit_1.clear()
            self.edit_2.clear()
        elif str(passwd) != str(rel_passwd):
            self.edit_2.clear()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.setWindowTitle('QLineEdit')
    window.setWindowIcon(QIcon('cats1.ico'))
    window.resize(300, 150)
    window.show()
    sys.exit(app.exec_())

在这里插入图片描述

3.8 输入限制

3.8.1 API

API功能备注
setMaxLength(int)
设置限制输入长度
内容长度限制
maxLength()
获取限制的输入长度
-
setReadOnly(bool)
设置只读限制
只读限制
isReadOnly()
获取只读限制
-
setValidator(QValidator)
设置验证器
规则验证
setInputMask(mask_str)
掩码验证
-
hasAcceptableInput()
判断输入文本是否通过验证
-

3.8.2 应用场景

限制用户在文本框中输入内容

3.8.3 案例1

完善3.7.3案例,添加内容长度限制

from PyQt5.Qt import *

import sys

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """控件调用方法"""
        # 用户名输入框
        self.label1 = QLabel(self)
        self.edit_1 = QLineEdit(self)
        self.label1.setText('用户名')
        self.label1.move(10, 13)
        self.edit_1.move(70, 10)
        self.edit_1.resize(200, 24)
        self.edit_1.setPlaceholderText('请输入用户名...')

        completer = QCompleter(['xujie', 'linfeng', '123456'], self.edit_1)
        self.edit_1.setCompleter(completer)
        # 设置最大长度
        self.edit_1.setMaxLength(10)


        # 密码输入框
        self.label2 = QLabel(self)
        self.edit_2 = QLineEdit(self)
        self.label2.setText('密码')
        self.label2.move(10, 53)
        self.edit_2.move(70, 50)
        self.edit_2.resize(200, 24)
        self.edit_2.setEchoMode(QLineEdit.Password)
        self.edit_2.setPlaceholderText('请输入密码...')
        self.edit_2.setClearButtonEnabled(True)

        # 添加密码框明文密文切换按钮,自定义操作
        action = QAction(self.edit_2)
        action.setIcon(QIcon('close_eye.png'))
        def change():
            """改变明文和密文方法"""
            if self.edit_2.echoMode() == QLineEdit.Normal:
                self.edit_2.setEchoMode(QLineEdit.Password)
                action.setIcon(QIcon('close_eye.png'))
            else:
                self.edit_2.setEchoMode(QLineEdit.Normal)
                action.setIcon(QIcon('open_eye.png'))
        action.triggered.connect(change)
        self.edit_2.addAction(action, QLineEdit.TrailingPosition)

        # 按钮控件
        self.button = QPushButton(self)
        self.button.setText('点击登录')
        self.button.move(80, 90)

        # 动作信号
        self.button.clicked.connect(self.get_command)

    def get_command(self):
        """按钮点击槽函数"""
        name = self.edit_1.text()
        passwd = self.edit_2.text()
        rel_name = 'xujie'
        rel_passwd = '123456'
        if name == rel_name and str(passwd) == str(rel_passwd):
            print('登录成功!!!')
        elif name != rel_name:
            self.edit_1.clear()
            self.edit_2.clear()
        elif str(passwd) != str(rel_passwd):
            self.edit_2.clear()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.setWindowTitle('QLineEdit')
    window.setWindowIcon(QIcon('cats1.ico'))
    window.resize(300, 150)
    window.show()
    sys.exit(app.exec_())

3.9 补充

3.9.1 QValidator

3.9.1.1 描述
3.9.1.1.1 验证器

用于验证用户输入数据的合法性

3.9.1.1.2 如果一个输入框设置了验证器
  1. 用户在文本框中输入内容时
  2. 首先会将内容传递给验证器进行验证
API返回值功能
validate(self, input_text_pos)
return (QVakidator.Acceptable, input_text, pos)
验证通过
return (QVakidator.Intermediate, input_text, pos)
暂不做验证
return (QVakidator.Invalid, input_text, pos)
验证不通过
  1. 是一个抽象类,使用前需要进行子类化操作
    1. 自定义子类
    2. 系统提供子类
API功能备注
QIntValidator(bottom, top, parent)
限制整形数据范围
-
QDoubleValidator(bottom, top, parent)
限制浮点型数据范围
经测试无效,得自定义实现
QRegExpValidator(bottom, top, parent)
通过正则表达式限定
-
3.9.1.2 基本使用
3.9.1.2.1 第一步

子类化此类

3.9.1.2.2 第二步:实现
API返回值功能
validate(self, input_text_pos)
return (QVakidator.Acceptable, input_text, pos)
验证通过
return (QVakidator.Intermediate, input_text, pos)
暂不做验证
return (QVakidator.Invalid, input_text, pos)
验证不通过
API返回值功能
fixup(self, input_text)
return修正后文本
修正
3.9.1.3 案例2

创建两个文本框
要求:

  1. 第一个文本框中添加限制,输入一个整形数字,如果数字范围在18-180之间,则输出该数字;
  2. 如果小于18或者为其他字符或为空,则输出为18;
  3. 如果大于180,则输出180;
  4. 切换到另一个输入框后,程序不报错,并按上三个要求自动填入数据在第一个文本框中
from PyQt5.Qt import *

import sys


class AgeVadidator(QValidator):
    """验证器类"""

    def validate(self, a0, a1):
        # 判断字符串是否以数字组成
        try:
            if 18 <= int(a0) <= 180:
                return QValidator.Acceptable, a0, a1
            elif 1 <= int(a0) <= 17:
                return QValidator.Intermediate, a0, a1
            else:
                return QValidator.Invalid, a0, a1
        except:
            if len(a0) == 0:
                return QValidator.Intermediate, a0, a1
            return QValidator.Invalid, a0, a1


    def fixup(self, a0):
        print(a0)
        try:
            if int(a0) <18:
                return '18'
            else:
                return '180'
        except:
            return '18'

class MyAgeValidator(QIntValidator):
    """数字验证器类"""
    def fixup(self, input):
        try:
            if int(input) < 18:
                return '18'
        except:
            return '18'

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('验证器的使用')
        self.resize(500, 500)
        self.initUI()

    def initUI(self):
        self.le = QLineEdit(self)
        self.le.move(100, 10)

        self.le2 = QLineEdit(self)
        self.le2.move(100, 50)

        # 创建验证器
        # 方式一:自定义验证器子类
        # vadidator = AgeVadidator()
        # 方式二
        vadidator = MyAgeValidator(18, 180)
        self.le.setValidator(vadidator)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()

    window.show()
    sys.exit(app.exec_())

3.9.2 掩码

3.9.2.1 描述
  1. 掩码可以指定固定位置的固定数据类型,达到一个格式上的限制
  2. 掩码由一串掩码字符和分隔符组成
  3. 可选的分号;和空白占位字符
3.9.2.2 举例
  1. 座机号码:四位区号-七位电话
  2. IP地址:XXX.XXX.XXX.XXX
3.9.2.3 掩码字符的含义

在这里插入图片描述

3.9.2.4 案例3

要求:

  1. 设置掩码总共5位字符,左边2(必须是大写字符) - 右边2(必须是数字)
  2. 使用#作为占位字符
from PyQt5.Qt import *

import sys

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QWidget()
    w.setWindowTitle('QLE使用')
    w.resize(500,500)

    # 创建文本框
    qle = QLineEdit(w)
    qle2 = QLineEdit(w)
    qle.move(10, 100)
    qle2.move(200,100)

    # 设置掩码
    qle.setInputMask('>AA-99;#')

    w.show()
    sys.exit(app.exec_())

在这里插入图片描述

3.10 是否被编辑

3.10.1 API

API功能备注
isModified()
查看是否被编辑
-
setModified(bool)
设置是否被编辑
-

3.10.2 应用场景

标识文本是否被修改

3.11 光标控制

3.11.1 API

API功能备注
cursorBackward(bool mark,int steps = 1)
向后(左)移动steps个字符
mark=True:带选中效果;
mark=False:不带选中效果
cursorForward(bool mark,int steps = 1)
向前(右)移动steps个字符
mark=True:带选中效果;
mark=False:不带选中效果
cursorWordBackward(bool mark)
向后(左)移动一个单词长度
mark=True:带选中效果;
mark=False:不带选中效果
cursorWordForward(bool mark)
向前(右)移动一个单词长度
mark=True:带选中效果;
mark=False:不带选中效果
home(bool)
移到行首
mark=True:带选中效果;
mark=False:不带选中效果
end(bool)
移到行尾
mark=True:带选中效果;
mark=False:不带选中效果
setCursorPosition(int)
设置光标位置
-
cursorPosition()
获取光标位置
-
cursorPositionAt(const QPoint & pos)
获取指定坐标位置对应文本光标位置
-

3.11.2 应用场景

控制光标,以及文本选择操作

3.11.3 案例

from PyQt5.Qt import *

import sys

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QWidget()
    w.setWindowTitle('光标使用')
    w.resize(500,500)

    # 创建文本框
    qle = QLineEdit(w)
    qle.move(10, 100)

    # 创建按钮
    btn = QPushButton(w)
    btn.setText('点我')
    btn.move(50, 130)

    def cursor_move():
        # qle.cursorBackward(True, 2)
        # qle.cursorWordBackward(True)
        qle.home(True)
        qle.setFocus()

    btn.clicked.connect(cursor_move)



    w.show()
    sys.exit(app.exec_())

3.12 文本边距设置

3.12.1 API

API功能备注
getTextMargins()
获取文本边距设置情况
-
setTextMargins(int left, int top, int right, int bottom)
获取文本边距设置情况
-

3.12.2 应用场景

设置文本内容边距

3.12.3 演示

from PyQt5.Qt import *

import sys

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QWidget()
    w.setWindowTitle('光标使用')
    w.resize(500,500)

    # 创建文本框
    qle = QLineEdit(w)
    qle.move(10, 50)
    qle.resize(300, 300)
    qle.setContentsMargins(100, 100, 0, 100)
    qle.setStyleSheet('background-color:cyan;')
    # qle.setTextMargins(100, 0, 0, 0)

    # 创建按钮
    btn = QPushButton(w)
    btn.setText('点我')
    btn.move(10, 10)

    w.show()
    sys.exit(app.exec_())

3.13 对齐方式

3.13.1 API

API功能备注
setAlignment(Qt.Alignment)
设置对齐文本方式
-
alignment -> Qt.Alignment
获取对齐文本方式
-

3.13.2 Qt.Alignment参数

方式参数解释
水平
Qt.AlignLeft
水平左对齐
Qt.AlignRight
水平右对齐
Qt.AlignHCenter
水平居中
Qt.AlignJustify
此处同左对齐
垂直
Qt.AlignTop
垂直靠上对齐
Qt.AlignBottom
垂直靠下对齐
Qt.AlignVCenter
垂直居中
Qt.AlignBaseline
此处同上对齐
垂直水平居中对齐
Qt.AlignCenter
等同于Qt.AlignHCenter | Qt.AlignVCenter

3.13.3 应用场景

设置用户内容文本对齐方式

3.14 常用编辑功能

3.14.1 API

API功能备注
backspace()
退格
删除选中文本或光标左侧的一个字符
del_()
删除
删除选中文本或者光标右侧一个字符
clear()
清空
清空文本框所有内容
copy()
复制
-
cut()
剪切
-
paste()
粘贴
-
undo()
撤销
isUndoAvailable(): 查看撤销状态
redo()
重做
isRedoAvailable(): 查看重做状态
setDragEnabled(bool)
设置选中文本后是否可以拖拽
-

3.14.2 应用场景

编辑用户输入文本

3.15 文本选中

API功能
setSelection(start_pos, length)
选中指定区间文本
selectAll()
选所有文本
deselect()
取消选中已选择文本
hasSelectedText()
是否有选中的文本
selectedText() → str
获取选中的文本
selectionStart() → int
选中开始位置
selectionEnd() → int
选中结束位置
selectionLength() → int
选中的长度

4. 信号

API功能备注
textEdited(text)
文本编辑时发射的信号
-
textChanged(text)
文本框文本发生改变时发射的信号
-
returnPressed()
按下回车键时发射的信号
-
editingFinished()
结束编辑时发射的信号
-
cursorPositionChanged(int oldPos, int newPos)
光标位置改变时发射的信号
-
selectionChanged(text)
选中文本发生改变时发射的信号
-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值