问题描述
我正在使用Qt 5.2,我想使 QLineEdit
不可编辑.问题是,它看起来不像它.使用 setReadOnly(true)
时,它保持白色背景,看起来仍然可以编辑.
I'm using Qt 5.2 and I would like to make a QLineEdit
not editable. The problem with this is, that it doesn't appear like it. When using setReadOnly(true)
it stays with white background and looks like it is still editable.
如果我禁用它,则它将变成灰色,并且文本也将变为浅灰色.问题是,在禁用状态下,无法从中复制文本.
If I disable it, then it turns gray and the text also gets a lighter gray. The problem is, that one can not copy the text from it, in a disabled state.
因此,如何使 QLineEdit
正确地不可编辑并使它看起来也是如此.在Windows中,此类控件通常为灰色,但文本保持黑色.当然,我可以手动设置样式,但这意味着它是硬编码的,在其他平台上可能看起来是错误的.
So how can I make a QLineEdit
properly non-editable and also make it look like it. In Windows such a control is usually gray, but the text stays black. Of course I could set the style manually, but this means that it is hard-coded and may look wrong on other platforms.
推荐答案
将行编辑为只读后,您可以将背景和文本颜色设置为您喜欢的任何颜色:
After making the line edit readonly, you can set the background and text colors to whatever you like :
ui->lineEdit->setReadOnly(true);
QPalette *palette = new QPalette();
palette->setColor(QPalette::Base,Qt::gray);
palette->setColor(QPalette::Text,Qt::darkGray);
ui->lineEdit->setPalette(*palette);
这篇关于如何使QLineEdit在Windows中不可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!