C++ 如何使 QLineEdit 在 Windows 中不可编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23915700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a QLineEdit not editable in Windows
提问by Devolus
I'm using Qt 5.2 and I would like to make a QLineEditnot 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.
我正在使用 Qt 5.2,我想制作一个QLineEdit不可编辑的。问题在于,它看起来不像。使用时setReadOnly(true)它保持白色背景,看起来仍然可以编辑。
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.
如果我禁用它,它就会变成灰色,文本也会变成浅灰色。问题是,在禁用状态下,无法从中复制文本。
So how can I make a QLineEditproperly 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.
那么我怎样才能制作一个QLineEdit正确的不可编辑的并使它看起来像它。在 Windows 中,这样的控件通常是灰色的,但文本保持黑色。当然我可以手动设置样式,但这意味着它是硬编码的,在其他平台上可能看起来不正确。
采纳答案by Nejat
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);
回答by Devolus
Since Nejat pointed me into the right direction with his answer, here is the code, that I now use:
由于 Nejat 的回答为我指明了正确的方向,这是我现在使用的代码:
QPalette mEditable = mGUI->mPathText->palette(); // Default colors
QPalette mNonEditable = mGUI->mPathText->palette();
QColor col = mNonEditable.color(QPalette::Button);
mNonEditable.setColor(QPalette::Base, col);
mNonEditable.setColor(QPalette::Text, Qt::black);
....
void MyWidget::setEditable(bool bEditable)
{
mGUI->mPathText->setReadOnly(!bEditable);
if(bEditable)
mGUI->mPathText->setPalette(mEditable);
else
mGUI->mPathText->setPalette(mNonEditable);
}
回答by Kai Walz
I had the same problem and made a subclass QLineViewderived from QLineEdit. Then, i reimplemented void setReadOnly(bool)and added a member var QPalette activePalette_
我遇到了同样的问题,并QLineView从QLineEdit. 然后,我重新实现void setReadOnly(bool)并添加了一个成员 varQPalette activePalette_
Store the QLineEdits palette within the ctor.
将QLineEdits 调色板存储在 ctor 中。
My reimplemented method lokks like this
我重新实现的方法就像这样
void QLineView::setReadOnly( bool state ) {
QLineEdit::setReadOnly(state);
if (state) {
QPalette pal = this->activePalette_;
QColor color = pal.color(QPalette::disabled, this->backgroundRole());
pal.setColor(QPalette::Active, this->backgroundRole(), color);
pal.setColor(QPalette::InActive, this->backgroundRole(), color);
this->setPalette(pal);
}
else {
this->setPalette(this->activePalette_);
}
}
回答by thuga
You could set a style sheet that changes the color of a QLineEditobject, if its readOnlyproperty is set to true.
QLineEdit如果对象的readOnly属性设置为 true ,您可以设置一个样式表来更改对象的颜色。
setStyleSheet("QLineEdit[readOnly=\"true\"] {"
"color: #808080;"
"background-color: #F0F0F0;"
"border: 1px solid #B0B0B0;"
"border-radius: 2px;}");

