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.
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.
You can disable QLineEdit using method "setEnabled ( false )":http://qt-project.org/doc/qt-4.8/qwidget.html#enabled-prop which is inherited from QWidget.
You can change the text with setText() or insert(). The text is retrieved with text(); the displayed text (which may be different, see EchoMode) is retrieved with displayText(). Text can be selected with setSelection() or selectAll(), and the selection can be cut(), copy()ied and paste()d.
As mentioned in the documentation, the text of a QLineEdit can be retrieved with its method text . Note that it's a QString , not a regular string, but that shouldn't be a problem as you format it with your "%s" % text .
The QLineEdit widget is a one-line text editor. A line edit allows the user to enter and edit a single line of plain text with a useful collection of editing functions, including undo and redo, cut and paste, and drag and drop.
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);
Since Nejat pointed me into the right direction with his answer, here is the code, that I now use:
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With