Get the color of a QT LCD Number

0

To insert the color in one of my LCD Number I simply use the instruction:

this->ui->lcdNumber_23->setPalette(Qt::red);

But I can not find how to get the current color of an LCD Number, I tried to get the palette but I can not find the get method. The objective is to make a comparison because depending on the current color of the number, I will put one or the other. Greetings.

    
asked by user3224353 24.05.2018 в 23:08
source

1 answer

2
  

I tried to get the palette but I can not find the get method

QLCDNumber inherits from QWidget and this object has a property called palette Looking there we see that it is a property of reading and writing. The writing is done, as you have said, with setPalette , while the reading is done with the method palette

 QPalette const& palette = ui->lcdNumber_23->palette();

Or, if you do not want to use references and want to copy the object:

QPalette palette = ui->lcdNumber_23->palette();

Following the comment:

  

Yes, that's how I get the palette, but then, how do I get the color of the palette, which is what I can not find?

Notice that when you call setPalette you are not passing it a palette as such but a QGlobalColor , which is an enumerated one. From that enumerated the compiler has to create an object of type QPalete , if it were not capable, an error would appear at compile time but it is not the case.

If we review, therefore, the documentation of QPalette , we see that it has a constructor that accepts a Qt::GlobalColor . The documentation of that method says the following:

  

Constructs a palette from the color button. The other colors are automatically calculated, based on this color. Window will be the button color as well.

That is, based on that color, the entire color palette is calculated. And the role that stores the color passed as an argument is Window ... it is enough to recover the color associated with that role to have the desired color. For this we have the method color :

QColor color = palette.color(QPalette::Window);

And ready, with that you should get the color you want.

    
answered by 24.05.2018 в 23:36