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. get the RGB hexadecimal color ( QColor)
Forum Updated to NodeBB v4.3 + New Features

get the RGB hexadecimal color ( QColor)

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 9.7k Views 2 Watching
  • 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.
  • Z Zunneh

    Hello guys,
    how to get the Hexadecimal RGB color value (see the screenshot) ? thanks
    0_1561020704903_paint.png

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #2

    @Zunneh
    QColor::rgba()
    if you want the string respentation as in your screenshot:

    QString("#%1").arg(QColor(...).rgba(), 8, 16)
    

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    Z 1 Reply Last reply
    3
    • raven-worxR raven-worx

      @Zunneh
      QColor::rgba()
      if you want the string respentation as in your screenshot:

      QString("#%1").arg(QColor(...).rgba(), 8, 16)
      
      Z Offline
      Z Offline
      Zunneh
      wrote on last edited by
      #3

      @raven-worx thank you , only one little problem, i get the Hexadecimal value + ff
      for exemple i select #90eeaa but when i click ok i get #ff90eeaa (where this ff come ? )
      another question : i want to get the binary value of this hexadecimal , can you help me ? thanks

      my english is average, please use simple words and try to be the most explicit, thank you

      raven-worxR 1 Reply Last reply
      0
      • Z Zunneh

        @raven-worx thank you , only one little problem, i get the Hexadecimal value + ff
        for exemple i select #90eeaa but when i click ok i get #ff90eeaa (where this ff come ? )
        another question : i want to get the binary value of this hexadecimal , can you help me ? thanks

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #4

        @Zunneh
        thats the alpha channel. Limit it to 6 characters then.

        QString("#%1").arg(QColor(...).rgb(), 6, 16)
        

        or compose it by concatenating each color component

        /QColor c(...);
        QString("#%1%2%3").arg(c.red(),2,16).arg(c.green(),2,16).arg(c.blue(),2,16)
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        Z 1 Reply Last reply
        3
        • raven-worxR raven-worx

          @Zunneh
          thats the alpha channel. Limit it to 6 characters then.

          QString("#%1").arg(QColor(...).rgb(), 6, 16)
          

          or compose it by concatenating each color component

          /QColor c(...);
          QString("#%1%2%3").arg(c.red(),2,16).arg(c.green(),2,16).arg(c.blue(),2,16)
          
          Z Offline
          Z Offline
          Zunneh
          wrote on last edited by Zunneh
          #5

          @raven-worx i changed it to 6 but always same problem
          i tried also to concetnating each color and convert from decimal to binary but imagine if one color is 0 , then he will write it 0 not 00000000 and the final result will be false

          my english is average, please use simple words and try to be the most explicit, thank you

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #6

            https://doc.qt.io/qt-5/qcolor.html#name

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            raven-worxR 1 Reply Last reply
            4
            • VRoninV VRonin

              https://doc.qt.io/qt-5/qcolor.html#name

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #7

              @Zunneh said in get the RGB hexadecimal color ( QColor):

              i changed it to 6 but always same problem

              yes i think the second parameter of QString::arg() doesnt limit the output and is only interpreted as minimum length.

              @VRonin
              does QColor::name() really always return the hex format? I thought i might also return "red" for example.
              I cant try myself right now

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              Z VRoninV 2 Replies Last reply
              1
              • raven-worxR raven-worx

                @Zunneh said in get the RGB hexadecimal color ( QColor):

                i changed it to 6 but always same problem

                yes i think the second parameter of QString::arg() doesnt limit the output and is only interpreted as minimum length.

                @VRonin
                does QColor::name() really always return the hex format? I thought i might also return "red" for example.
                I cant try myself right now

                Z Offline
                Z Offline
                Zunneh
                wrote on last edited by
                #8

                @raven-worx it returned the Hex format but in QString, so now i have to convert it to Hex and then to binary (because i want my output in binary )
                @VRonin is there a function that returned the color in Hexadecimal and not in a QString ? thanks

                my english is average, please use simple words and try to be the most explicit, thank you

                raven-worxR 1 Reply Last reply
                0
                • raven-worxR raven-worx

                  @Zunneh said in get the RGB hexadecimal color ( QColor):

                  i changed it to 6 but always same problem

                  yes i think the second parameter of QString::arg() doesnt limit the output and is only interpreted as minimum length.

                  @VRonin
                  does QColor::name() really always return the hex format? I thought i might also return "red" for example.
                  I cant try myself right now

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #9

                  @raven-worx said in get the RGB hexadecimal color ( QColor):

                  does QColor::name() really always return the hex format? I thought i might also return "red" for example.

                  qDebug() << QColor(Qt::red).name(); returns "#ff0000"

                  @Zunneh said in get the RGB hexadecimal color ( QColor):

                  is there a function that returned the color in Hexadecimal and not in a QString ?

                  😓 for the numeric representation you can just use unsigned int colNum = QColor(Qt::red).rgb()

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  2
                  • Z Zunneh

                    @raven-worx it returned the Hex format but in QString, so now i have to convert it to Hex and then to binary (because i want my output in binary )
                    @VRonin is there a function that returned the color in Hexadecimal and not in a QString ? thanks

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by raven-worx
                    #10

                    @Zunneh said in get the RGB hexadecimal color ( QColor):

                    it returned the Hex format but in QString, so now i have to convert it to Hex and then to binary (because i want my output in binary )

                    what is binary in your case?!
                    QColor::rgb() returns a 32bit value... which contains each color component byte by byte. So its is binary if you want so.

                    something like this?

                    QByteArray a = QByteArray("#") + QByteArray::number(c.red(),16) + QByteArray::number(c.green(),16) + QByteArray::number(c.blue(),16);
                    a.toHex()
                    

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    3
                    • Z Offline
                      Z Offline
                      Zunneh
                      wrote on last edited by
                      #11

                      i tried this and it work , thanks guys

                      bool ok;
                      QColor color;
                      color = QColorDialog::getColor(Qt::white,this); 
                      QString ColorHex=color.name().remove('#');
                      int ColorInt = ColorHex.toInt(&ok,16);
                      QString ColorBin= ColorHex.setNum(ColorInt, 2);
                      lab->setText(ColorBin);
                      

                      my english is average, please use simple words and try to be the most explicit, thank you

                      VRoninV 1 Reply Last reply
                      0
                      • Z Zunneh

                        i tried this and it work , thanks guys

                        bool ok;
                        QColor color;
                        color = QColorDialog::getColor(Qt::white,this); 
                        QString ColorHex=color.name().remove('#');
                        int ColorInt = ColorHex.toInt(&ok,16);
                        QString ColorBin= ColorHex.setNum(ColorInt, 2);
                        lab->setText(ColorBin);
                        
                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #12

                        @Zunneh said in get the RGB hexadecimal color ( QColor):

                        i tried this

                        But Why?!

                        int ColorInt =QColorDialog::getColor(Qt::white,this).rgb()
                        lab->setText(QString::number(ColorInt ,2));
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        0

                        • Login

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