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. [SOLVED]QVariant will drop alpha value when save QColor?

[SOLVED]QVariant will drop alpha value when save QColor?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 7.8k Views 1 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.
  • L Offline
    L Offline
    liuyanghejerry
    wrote on last edited by
    #1

    It seems that QVariant only save QColor as #RRGGBB, and all other info will no longer exists, including alpha value of QColor.

    So, any better way to work around this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      If you save the color in a text file, you could test for the QColor type and make the string yourself
      Assuming you are using a QTextStream;

      @

      if (variant.type() == QVariant::Color) {
      QColor color = variant.value<QColor>();
      QString colorText = QString("#%1%2%3%4").arg(color.red())
      .arg(color.green())
      .arg(color.blue())
      .arg(color.alpha());
      stream << colorText;
      }
      @

      And then parse the string in using a similar logic when you read the file

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        liuyanghejerry
        wrote on last edited by
        #3

        Thanks~

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dvdk
          wrote on last edited by
          #4

          If you only want to save RGBA values in a QVariant, you can save and restore like this with less overhead :
          @
          //Store
          QColor color;
          QVariant v(color.rgba());

          //Retrieve
          QColor color = QColor::fromRgba(v.toUInt());
          @

          note though that the QColor is stored as a 32bit unsigned integer.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            liuyanghejerry
            wrote on last edited by
            #5

            Indeed, saving as integer is more efficient. I've changed my code to this^^

            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