PDA

View Full Version : Qt4/C++ - Find colour of text in a QPushButton



jimbo
18th May 2015, 17:41
Hello,

Linux - Qt 4.8.2

I've an array of QPushButtons, I can change the text colour of a button (no problems).
In my program I have a 'GO' QPushbutton that takes a different action, depending on the text
colour of the buttons in the array.
I can't find a way to determine the button text colour.

if (eeButtons[3]->foregroundRole() == Qt::red) {
qDebug() << "RED";
}This compiles but does nothing, What should I put between the () in foregroundRole()?

I've found this.
QString test = eeButtons[3]->styleSheet();
qDebug() << test.toAscii().data();

output:-
* { color: rgb(255, 0, 0) } //red
This is probably the wrong approach. (Suggestions please)
I can get it working, by setting flags on colour changes (just a bit fiddly).

Regards

stampede
18th May 2015, 17:58
This is probably the wrong approach.
I agree. I think your whole logic is kind of "reversed" - your button should display a colour corresponding to some internal state of your application, and the program logic should follow that internal state, and not the colour of a ui control.

I can get it working, by setting flags on colour changes (just a bit fiddly).
In my opinion relying on button colour to code ui behaviour seems more "fiddly" ;)
Try to abstract the program logic from the ui controls - normally you think "It is safe to close the app when the document is saved" and not "It is safe to close the app when the document status bar goes green". I think you get the point.

jimbo
18th May 2015, 18:25
Hello,

Thanks for your reply.

I think you get the pointOK I'll use flags.

Regards