Important: Please read the Qt Code of Conduct -

Pass Qt::green like function argument

  • 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::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?


  • Moderators

    Well Qt::green is one of the values of a Qt::GlobalColor enum type, so your function signature would be

    void 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;

    The include you need for that is <QColor>.

  • Thank you very much

Log in to reply