Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Make Line Edit Uneditable and Change the background color



  • Hi,

    I cant figure out how to make a Line Edit uneditable. Basically I dont want the user to be able to change the content by clicking on the widget edit it with his keyboard, because I have a set of buttons that allow specific changes.

    My second question is how to change the background color from the default white, i didnt see the option under widget properties. Thanks



  • 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 background color using "QPalette":http://qt-project.org/doc/qt-4.8/qpalette.html. The source code should be something like:
    @
    QPalette palette = lineEdit.palette();
    palette.setColor(QPalette::Base, QColor(255,0,0)); //Red
    lineEdit.setPalette(palette);
    @


  • Moderators

    Is there some reason, then, to have the text in a QLineEdit? Perhaps a QLabel would be your better option. It's always better to use a widget for its intended use, rather than try to shoehorn in different functionality that the user might not expect.

    With that said, you're probably looking for @lineEdit->setReadOnly(true);@

    And for your second issue, you can use "stylesheets":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qabstractscrollarea to change background-color.



  • [quote author="mlong" date="1331135892"]Is there some reason, then, to have the text in a QLineEdit? Perhaps a QLabel would be your better option. [/quote]

    I guess that the goal is to enable/disable the QLineEdit instance depending the status of the application.


  • Moderators

    I would think so, but wasn't sure because of the context of

    bq. I have a set of buttons that allow specific changes.

    If that means that a particular button enables/disables a QLineEdit, then it makes sense. But if it means that you click a button to make the text something predefined (which just needs to be displayed) then it may not be the best solution.



  • [quote author="mlong" date="1331136920"]If that means that a particular button enables/disables a QLineEdit, then it makes sense. But if it means that you click a button to make the text something predefined (which just needs to be displayed) then it may not be the best solution.
    [/quote]

    Yes, absolutely. I totally agree that in the second case it would be better to use QLabel.


Log in to reply