JenyaKh
JenyaKh

Reputation: 2498

Get Qt default style for QLineEdit to change its border color only

I would like to change the frame color of QLineEdit to red.

If I do it like this:

_lineEdit->setStyleSheet("border : 1px solid red");

the shape of the line edit is changed from its default and the border color change on focus stops happening.

My idea is to take the default values for Qt colors and shapes of QLineEdit and set them using setStyleSheet() but with a border color being changed to red. But how can I get the values programmatically?

I have seen the question How to change QLineEdit border color only , but it is not answered.

Upvotes: 0

Views: 1415

Answers (3)

Neph
Neph

Reputation: 2001

I just came across this problem with my Python script and found an old answer that solved it for me:

Don't try to restore the exact look with a custom style sheet, instead simply save the original one, then restore it when necessary. In Python this looks like this:

lineedit = QLineEdit()
lineedit.setText("Some Text")

if some_condition: #Set red border
    self.oldstyle = lineedit.styleSheet()
    lineedit.setStyleSheet("border: 1px solid red")
else: #Restore original
    lineedit.setStyleSheet(self.oldstyle)

In my tests the color change on focus was working as expected afterwards.

According to the C++ Qt docs for QWidget, style sheets are supported in that version too, so it should work the same way.

Upvotes: 0

ff00000000x00
ff00000000x00

Reputation: 34

Not really understood what you want, but if I get it you need:

line.setStyleSheet("border : 1px solid red; padding-top: 2px; padding-bottom: 2px; border-radius: 2px");

If you need work with object state, look here -> https://doc.qt.io/qt-5/stylesheet-reference.html

you need the "List of Pseudo-States"

Upvotes: 1

cyberfish7
cyberfish7

Reputation: 11

For us it worked to set the properties individually. No default values need to be known.
Here is an example where we change the frame color on mouse over:

lineEdit->setStyleSheet("QLineEdit {border-width: 1px; border-style: solid; border-color: red;}"
                        "QLineEdit:hover {border-width: 1px; border-style: solid; border-color: blue;}");

I hope this is helpful.

Upvotes: 1

Related Questions