Qt 设置QLineEdit控件placeholderText颜色

Qt 会根据QLineEdit控件显示文本的颜色自动设置placeholderText颜色,如果想自定义placeholderText颜色,可以通过以下方法。

在样式文件中增加以下设置:

cpp 复制代码
QLineEdit#lineEdit_userName,
QLineEdit#lineEdit_password{
    border: none;
    padding: 6px 6px;
    background-color: transparent;
    color: #FFFFFF;
    font-size: 10.5pt; 
    font-family: Microsoft YaHei; 
    font-weight: 500; 
}

QLineEdit#lineEdit_userName[text=""],
QLineEdit#lineEdit_password[text=""]{ /* 设置 placeholderText 颜色*/
    color: #868C99; 
}

在源码文件中增加以下代码:

cpp 复制代码
// 使样式表中对 placeholderText 的颜色设置生效
connect(ui->lineEdit_userName, &QLineEdit::textChanged, [=]{style()->polish(ui->lineEdit_userName);});
connect(ui->lineEdit_password, &QLineEdit::textChanged, [=]{style()->polish(ui->lineEdit_password);});
相关推荐
xcyxiner6 小时前
DicomViewer (添加模型类)3
qt
xcyxiner1 天前
DicomViewer (目录调整) 2
qt
xcyxiner1 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
clint4562 天前
C++进阶(1)——前景提要
c++
夜悊2 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴2 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0013 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾3 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
桥田智能3 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
один but you3 天前
constexpr函数
c++