How to make a QLineEdit not editable in Windows

33,910

Solution 1

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);

Solution 2

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);
}

Solution 3

You could set a style sheet that changes the color of a QLineEdit object, if its readOnly property is set to true.

setStyleSheet("QLineEdit[readOnly=\"true\"] {"
              "color: #808080;"
              "background-color: #F0F0F0;"
              "border: 1px solid #B0B0B0;"
              "border-radius: 2px;}");

Solution 4

I had the same problem and made a subclass QLineView derived from QLineEdit. Then, i reimplemented void setReadOnly(bool) and added a member var QPalette activePalette_

Store the QLineEdits palette within the 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_);
    }
}
Share:
33,910

Related videos on Youtube

Devolus
Author by

Devolus

Assembly, C/C++, SQL and Java developer. Been working in the industry for over 20 years now, and still love programming for a hobby.

Updated on May 03, 2020

Comments

  • Devolus
    Devolus about 4 years

    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.

    • LtWorf
      LtWorf almost 10 years
      Gray is for "disabled" widgets, not for read-only ones. They are different ideas.
  • Devolus
    Devolus almost 10 years
    It's not exactly what I wanted, but it led me into the right direction. :) Thx.
  • eric
    eric over 9 years
    Why downvoted? I did not downvote it, but my hunch is it is not needed, as you could do the above solutions, or just use qlabel (with changed properties) without making a new subclass.
  • Devolus
    Devolus over 9 years
    But can a label also display complex text like HTML with formatting?
  • Mad Physicist
    Mad Physicist about 7 years
    Probably because you gave credit where it's due and someone didn't read your answer all the way through... Nice answer BTW.
  • Mad Physicist
    Mad Physicist about 7 years
    My only suggestion would be to change QPalette::Button to QPalette::Window and Qt::black to QPalette::WindowText for portability.