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. How does the "toString" method of qt data structures work?
Qt 6.11 is out! See what's new in the release blog

How does the "toString" method of qt data structures work?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.9k 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.
  • Q Offline
    Q Offline
    QAndrew
    wrote on last edited by QAndrew
    #1

    I found myself trying to save the textual representation of a QVariantMap (QMap<QString, QVariantMap>> actually) like as if I were to write
    qDebug() << myMap
    which yields the correct text data put to the console, but instead have the result in a file. I tried with QDataStream class but it can only handle binary form, I managed to do it finally like this:
    QDebug outDebug(&file);
    outDebug << myMap;
    But it really bothers me that I have no idea what is going on behind the scenes. I assume either QDebug & QDebug::operator<<(const QByteArray & b) or QDebug & QDebug::operator<<(const QString & s) functions are called after an implicit conversion, but how does that implicit conversion work?

    Also I have a custom (non-QObject)! class that is among the stored QVariants and it's textual repesentation is an empty string. Is there a way to customize that?

    1 Reply Last reply
    0
    • FranckynosF Offline
      FranckynosF Offline
      Franckynos
      wrote on last edited by
      #2

      YourClass
      {
      friend QDataStream & operator << (QDataStream & out, const YourClass& t)
      {
      out << t._nom;
      return out;
      }
      };

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QAndrew
        wrote on last edited by
        #3

        That'll work, ty, but how come a QDebug instance can decompress the binary to textual? And how does one do it without QDebug instance?

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          QAndrew
          wrote on last edited by QAndrew
          #4

          And actually I can't make your code snippet work. I implemented the friend operator method as described by you and doc but it's not called like it should be.

          QVariant(BitPattern, )
          empty string................^

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

            Hi,

            Have a look at the QDebug documentation, there's an example on how to write a debug stream operator for your custom type.

            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
            • Q Offline
              Q Offline
              QAndrew
              wrote on last edited by QAndrew
              #6

              Aha, I see, somehow I skipped trough that paragraph of the doc, thanks!

              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