Pass Qt::green like function argument
-
wrote on 28 May 2016, 19:01 last edited by
Hello and thank you very much for all.
I would like to create a setter to update the color of a QPainter Object...this is my idea...
void ampel::updateColor(){ <----- how to pass here Qt::greencColor->setColor(Qt::green);
}
but, how can I pass the Qt::green like a variable, which type of variable, musst I use? Is needed a special #include in the MainWindow, for Qt:green like valour use?
Greetings
-
Well
Qt::green
is one of the values of a Qt::GlobalColor enum type, so your function signature would bevoid ampel::updateColor(Qt::GlobalColor color)
but this is not a good idea.
It's not a good idea because it limits your colors to the predefined constants, and that limitation is artificial. What you really want to do is pass any color and Qt has a QColor class for that. This is also the argument of
setColor
so matching signature of your surrounding function could potentially mean one conversion less.
So I propose you give it the following signature:void ampel::updateColor(const QColor& color)
Thanks to the multiple constructors of QColor and the rules of implicit conversion of c++ this can be then called in any of these ways:
ampel foo; foo.updateColor(Qt::green); foo.updateColor(QColor(0,255,0)); foo.updateColor("green"); foo.updateColor("#00FF00"); foo.updateColor(0x00FF00);
The include you need for that is
<QColor>
. -
wrote on 28 May 2016, 21:50 last edited by
Thank you very much
1/3