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 QColor and QSize from and to QString
Qt 6.11 is out! See what's new in the release blog

Convert QColor and QSize from and to QString

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 13.7k 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.
  • V Offline
    V Offline
    viktor.benei
    wrote on last edited by
    #1

    Probably it's an easy thing, but I searched a lot (Google, Qt forum, stackoverflow...) and didn't find the answer.

    So my problem is: I have QColor and QSize variables I have to convert to QString, and then later back to the related type (QColor or QSize).

    I tried a lot of different approaches but no luck so far.

    Thanks for your time.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      you could use the serialization of these objects, if you want to store as a osrt of backup.
      For "QColor":http://doc.qt.nokia.com/4.7/qcolor.html#operator-lt-lt-201 and "QSize":http://doc.qt.nokia.com/4.7/qsize.html#operator-gt-gt-99

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • V Offline
        V Offline
        viktor.benei
        wrote on last edited by
        #3

        I have to have them in presentable text (QString) format.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Ruu_Rinki
          wrote on last edited by
          #4

          Hello.
          QColor to QString (method QString QColor::name)
          http://doc.qt.nokia.com/latest/qcolor.html#name
          QString to QColor: you should used constructor QColor(QString &name);

          I don't now how to directly convert from QSize to QString. But I suggest you get int value height and width and convert them to QString.
          (method QString::setNum(int))

          1 Reply Last reply
          2
          • V Offline
            V Offline
            viktor.benei
            wrote on last edited by
            #5

            The QColor to QString and vice versa version is great, thanks, but I would prefer a more unified solution. If I qDebug a QVariant holding QSize or QColor it can print it like

            QVariant(QColor, QColor(ARGB 1, 0.784314, 0.392157, 0.392157) )

            I would like to do something similar, but I have to convert the value back as well.

            [quote author="Ruu_Rinki" date="1319465978"]Hello.
            QColor to QString (method QString QColor::name)
            http://doc.qt.nokia.com/latest/qcolor.html#name
            QString to QColor: you should used constructor QColor(QString &name);

            I don't now how to directly convert from QSize to QString. But I suggest you get int value height and width and convert them to QString.
            (method QString::setNum(int))[/quote]

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              There are no built in methods for QColor for doing that. You'll have to write your own. You might want to take the implementation for the [[Doc:QDataStream]] support as a start.

              http://www.catb.org/~esr/faqs/smart-questions.html

              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