要将 QLineEdit 的框置为灰色,可以使用 QPalette 将其 Disabled 状态下的背景颜色设置为灰色。以下是一个示例代码:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout *layout = new QVBoxLayout(&window);
QLineEdit *lineEdit = new QLineEdit("Text");
// 获取当前的调色板
QPalette palette = lineEdit->palette();
// 设置 Line Edit 的 Disabled 状态下背景颜色为灰色
palette.setColor(QPalette::Disabled, QPalette::Base, Qt::gray);
lineEdit->setPalette(palette);
layout->addWidget(lineEdit);
window.show();
return app.exec();
}
在上述代码中,我们获取了 QLineEdit 的当前调色板,并使用 QPalette::setColor()
方法来设置其 Disabled 状态下的背景颜色(QPalette::Base
)为灰色(Qt::gray
)。
你可以根据需要调整其他状态和颜色组来实现不同的效果。
内容由零声教学AI助手提供,问题来源于学员提问