[Interest] QWidget style change and reverting it back

Thiago Macieira thiago.macieira at intel.com
Tue Feb 9 16:47:54 CET 2016


On terça-feira, 9 de fevereiro de 2016 18:31:54 PST Prav wrote:
> I am trying to change color of QLineEdit with
> 
> ui->lineEdit->setStyleSheet("color: red;");

Why didn't you just set the colour, by changing the widget's palette?

> 
> How  that could be that doing the same thing second time I got changed
> size of lineEdit? ... Windows, Qt 5.5.1 ... test project is inside zip-file.
> 
> 
> And  how  to  make lineEdit look as it was before using it's setStyleSheet()
> method? ui->lineEdit->setStyleSheet("");
> does not work for me.
> 
> Or using setStyleSheet() is irreversible operation?
> If so how to change color of widget and then return it back.

It has an irreversible consequence, that of using QStyleSheetStyle for the 
widget's style, instead of whatever is the default for your platform (in case 
of a modern Windows, that's QWindowsVistaStyle, provided in the windows.dll 
platform plugin). The QStyleSheetStyle class is supposed to mimic the 
platform's style, but it exists in the first place in order to do things that 
are not possible with the platform's style.

So if you use stylesheets, you *accept* that your widget will look different 
from the platform. That is not a bug. What's more, changing the style implies 
you don't want to look like the native look and feel anyway.

We can say there's room for improvement, though. See https://bugreports.qt.io/
browse/QTBUG-50976 for an example.

My personal recommendation: don't use style sheets, at all.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list