Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pass Qt::green like function argument
Forum Update on Monday, May 27th 2025

Pass Qt::green like function argument

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yczo
    wrote on 28 May 2016, 19:01 last edited by
    #1

    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

    cColor->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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 28 May 2016, 19:20 last edited by Chris Kawa
      #2

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

      1 Reply Last reply
      2
      • Y Offline
        Y Offline
        yczo
        wrote on 28 May 2016, 21:50 last edited by
        #3

        Thank you very much

        1 Reply Last reply
        0

        1/3

        28 May 2016, 19:01

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved