You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt 6.2圆角TextField文本不居中及密码模式圆点过大问题求解

修复方案

1 文本垂直偏移问题修复

核心问题为自定义背景未锚定填充父组件,且Qt 6.2 Android端TextField的默认内容对齐逻辑存在兼容性问题,仅设置verticalAlignment不生效。
修改后的通用TextField代码如下:

TextField {
    id: usernameTextField
    placeholderText: qsTr("username")
    width: 250
    height: 40
    anchors {
        top: parent.top
        topMargin: giveDetailsLabel.anchors.topMargin + 36
        horizontalCenter: parent.horizontalCenter
        horizontalCenterOffset: 0
    }

    background: Rectangle {
        anchors.fill: parent // 新增:背景填充父组件
        radius: 20
        border.color: "#C9C9C9"
        border.width: 1
    }

    // 新增:显式设置上下内边距,自动适配高度与字号
    leftPadding: 10
    rightPadding: 10
    topPadding: (height - font.pixelSize) / 2
    bottomPadding: topPadding

    // 替换原有verticalAlignment,直接在内容项配置对齐逻辑,兼容性更强
    contentItem: TextInput {
        font: parent.font
        text: parent.text
        placeholderText: parent.placeholderText
        verticalAlignment: Text.AlignVCenter
        leftPadding: parent.leftPadding
        rightPadding: parent.rightPadding
        topPadding: parent.topPadding
        bottomPadding: parent.bottomPadding
        selectionColor: parent.selectionColor
        selectedTextColor: parent.selectedTextColor
    }

    font.pixelSize: 16
    font.weight: Font.Normal
    font.family: "Open Sans"
}

以上修改可同时解决普通输入框、密码输入框的文本/圆点垂直偏移问题。

2 密码圆点过大问题修复

Qt默认密码圆点尺寸与字号直接绑定,要实现单独调整圆点大小且不影响输入时明文显示效果,按以下配置修改密码输入框:

TextField {
    id: passwordTextField
    echoMode: TextField.Password
    // 复用上述通用TextField的宽度、锚定、背景、内边距等基础配置
    width: 250
    height: 40
    // ... 其余通用配置保持和上面一致

    contentItem: TextInput {
        font: parent.font
        text: parent.text
        placeholderText: parent.placeholderText
        verticalAlignment: Text.AlignVCenter
        leftPadding: parent.leftPadding
        rightPadding: parent.rightPadding
        topPadding: parent.topPadding
        bottomPadding: parent.bottomPadding
        selectionColor: parent.selectionColor
        selectedTextColor: parent.selectedTextColor

        echoMode: parent.echoMode
        // 替换默认大圆点为更小的圆点字符,可根据需求调整字符样式
        passwordCharacter: "·"
    }
}

内容的提问来源于stack exchange,提问作者user7179690

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 01:15:01