如何使QLineEdit在Windows中不可编辑
How to make a QLineEdit not editable in Windows
我使用Qt 5.2,我想使QLineEdit
不可编辑。问题是,它看起来不像。当使用setReadOnly(true)
时,它保持白色背景,看起来仍然是可编辑的。
如果我禁用它,然后它变成灰色,文本也得到浅灰色。问题是,在禁用状态下,无法从中复制文本。
那么我怎样才能使QLineEdit
正确地不可编辑,也使它看起来像它。在Windows中,这样的控件通常是灰色的,但文本保持黑色。当然,我可以手动设置样式,但这意味着它是硬编码的,在其他平台上可能看起来不正确。
将行编辑设置为只读后,您可以将背景和文本颜色设置为您喜欢的任何颜色:
ui->lineEdit->setReadOnly(true);
QPalette *palette = new QPalette();
palette->setColor(QPalette::Base,Qt::gray);
palette->setColor(QPalette::Text,Qt::darkGray);
ui->lineEdit->setPalette(*palette);
既然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);
}
如果readOnly
属性设置为true,则可以设置样式表来更改QLineEdit
对象的颜色。
setStyleSheet("QLineEdit[readOnly="true"] {"
"color: #808080;"
"background-color: #F0F0F0;"
"border: 1px solid #B0B0B0;"
"border-radius: 2px;}");
我有同样的问题,并从QLineEdit
派生了一个子类QLineView
。然后,我重新实现了void setReadOnly(bool)
,并添加了成员变量QPalette activePalette_
将 QLineEdit
s调色板存储在函数中。
我重新实现的方法看起来像这样
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_);
}
}
相关文章:
- 如何在Windows中将使用Qt制作的记事本设置为默认文本编辑器
- (编辑)如何在Windows中导出模板专业,并在CPP文件中声明源
- 读取 USB 相机的输入编辑并将输出发送到 Windows 上的虚拟相机
- QTreeWidget:类似于Windows资源管理器的编辑
- 有没有一种方法可以在linux中编辑windows C++exe文件
- 使用 C++ 获取默认的 Windows 文本编辑器.exe路径
- 在Windows 7中为任何编辑器安装OpenCV 2.4.7
- 如何在没有Windows API的情况下在Turbo-C++中制作一个简单的文本编辑器
- 使用脚本编辑 Windows xp 注册表
- C++ Windows CE 中的编辑框对齐
- 检索 Windows 资源管理器的地址栏编辑控件的句柄
- 如何从windows编辑控件的格式中删除逗号?(C++)
- Windows编辑启动应用程序C/C++
- 强制windows在编辑FAT32根目录信息后重新加载
- 拦截Windows文件系统,编辑数据
- 一种在Windows中用C++编辑(读/写文本)现有.doc(MS Word)文件的方法
- 通过Winapi以编程方式编辑Windows审核策略时出现权限错误
- 如何使QLineEdit在Windows中不可编辑
- 任何 Windows 终端的调色板都可以使用 pdcurses 进行编辑吗?
- C++Windows窗体应用程序-XML编辑现有的XML子节点