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. Convert QString to QColor ( "CR_RED" to QColor(CR_RED))
Forum Updated to NodeBB v4.3 + New Features

Convert QString to QColor ( "CR_RED" to QColor(CR_RED))

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 4.3k 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.
  • N Offline
    N Offline
    ndiwan
    wrote on last edited by ndiwan
    #1

    Convert QString to QColor ( "CR_RED" to QColor(CR_RED))

    Is there any way we can convert a QString("CR_RED") that I am getting from XML file to color code or rgb string ?
    Here is the code:
    if(locked.tagName() == "objects")
    {
    QString colorName = locked.attribute("color","");
    QString assetName = locked.firstChild().toText().data();
    if(assetName.compare("switch")== 0)
    {
    for(int i=0;i<m_SwitchList->count();i++)
    {
    m_SwitchList->at(i)->SetSwitchLockedColor(colorName );
    }
    }
    }

    Here I need to pass colorName to SetSwitchLockedColor(), but colorName is QString. I need to convert it into QColor or rgb string.

    ODБOïO 1 Reply Last reply
    0
    • N ndiwan

      Convert QString to QColor ( "CR_RED" to QColor(CR_RED))

      Is there any way we can convert a QString("CR_RED") that I am getting from XML file to color code or rgb string ?
      Here is the code:
      if(locked.tagName() == "objects")
      {
      QString colorName = locked.attribute("color","");
      QString assetName = locked.firstChild().toText().data();
      if(assetName.compare("switch")== 0)
      {
      for(int i=0;i<m_SwitchList->count();i++)
      {
      m_SwitchList->at(i)->SetSwitchLockedColor(colorName );
      }
      }
      }

      Here I need to pass colorName to SetSwitchLockedColor(), but colorName is QString. I need to convert it into QColor or rgb string.

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      hi @ndiwan

      you can construct a QColor from QString
      https://doc.qt.io/qt-5/qcolor.html#QColor-5

      1 Reply Last reply
      1
      • N Offline
        N Offline
        ndiwan
        wrote on last edited by
        #3

        Thanks. I used QColor.setNamedColor(QString.toLatin1()). It worked.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          ndiwan
          wrote on last edited by
          #4

          I am Sorry its not yet working.
          I can use QColor::setNamedColor(QString) but it need rgb string. But I have normal QString("CR_RED").

          Vice-verca is working.

          QColor color = CR_RED;
          QString rgbString = color.name();

          Now I can use QColor::setNamedColor(QString) to convert rgbString to QColor.
          But i want to convert QString("CR_RED") into QColor/rgb QString

          Thanks

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @ndiwan said in Convert QString to QColor ( "CR_RED" to QColor(CR_RED)):

            CR_RED

            This is not a valid color name for QColor.setNamedColor(). You have to add your own parser for your color names.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3

            • Login

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