Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a QLineEdit not editable in Windows

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.

like image 842
Devolus Avatar asked May 28 '14 15:05

Devolus


People also ask

How do I make QLineEdit not editable?

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.

How do you edit text in QLineEdit?

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.

How do I read text in QLineEdit?

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 .

What is a QLineEdit?

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.


2 Answers

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); 
like image 192
Nejat Avatar answered Oct 01 '22 10:10

Nejat


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); } 
like image 20
Devolus Avatar answered Oct 01 '22 09:10

Devolus