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. Read and write QBrush (QPen, QFont) as XML
QtWS25 Last Chance

Read and write QBrush (QPen, QFont) as XML

Scheduled Pinned Locked Moved Unsolved General and Desktop
qbrushqpenqfontxmlserialization
6 Posts 2 Posters 2.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.
  • T Offline
    T Offline
    TheTrueGoofy
    wrote on last edited by
    #1

    I'd like to persist QBrush, QPen, QFont in an XML-file.
    Is there a simple solution available based on QDomDocument?

    While QFont has "toString" and "fromString", the QBrush or QPen don't have this methods. There seems to be a solution based on serializing them as QDataStream, but I can't get it working with a human readable text. The output of QBrush as QDebug looks quite nice, but I guess that's not the proper way to do it :

    QString str;
    QDebug debug(&str);
    debug.nospace();
    debug.noquote();
    debug << QBrush(QColor(250,150,0,100),Qt::CrossPattern);
    

    The code above works well, and the value of "str" will be "QBrush(QColor(ARGB 0.392157, 0.980392, 0.588235, 0),CrossPattern)" ... That appears well readable, but I've no clue how to do the reverse parsing, nor do I think that QDebug should be needed. Ideally the code would look like this (example below does not compile):

    QDomElement element = domdocument.createElement("myelement");
    element.setAttribute("mybrush", QBrush(QColor(250,150,0,100),Qt::CrossPattern));
    QBrush brush(element.attribute("mybrush"));
    

    Any idea how to do this?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      QDataStream is the way to go and definitely does work. Are you certain you need the data to be human-readable?

      If yes, then I list an idea below (but I have no idea if it will work):

      Use QVariant

      QDomElement element = domdocument.createElement("myelement");
      const QVariant myBrush(QColor(250,150,0,100),Qt::CrossPattern));
      element.setAttribute("mybrush", myBrush); // or myBrush.toString() maybe?
      QBrush brush(element.attribute("mybrush"));
      

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TheTrueGoofy
        wrote on last edited by
        #3

        A major benefit of XML consists certainly in readability for machine AND human. I'd be happy to use the QDataStream, if the data would be stored in a binary file-format. I was trying to use the QVariant, but unfortunately the output of the code below is an empty string. :-(

          QBrush brush(QColor(250,150,0,100),Qt::CrossPattern);
          QVariant variant_brush(brush);
          std::cout << "variant_brush = \"" << variant_brush.toString().toStdString() << "\“\n";
        
        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Try dividing the brush into it's constituents, then, perhaps? I mean store QColor, pattern and width separately.

          (Z(:^

          T 1 Reply Last reply
          1
          • sierdzioS sierdzio

            Try dividing the brush into it's constituents, then, perhaps? I mean store QColor, pattern and width separately.

            T Offline
            T Offline
            TheTrueGoofy
            wrote on last edited by
            #5

            @sierdzio

            That's what I had in mind too (as a fallback-scenario). Certainly this is also quite simple to implement, but I was hoping that there is a more elegant solution.

            QDomElement createDomElement(QDomDocument& domDocument,
                                         const QBrush& brush,
                                         const QString& tagName = "QBrush")
            {
              QDomElement element = domDocument.createElement(tagName);
              element.setAttribute("QBrushColorARGB", "#" + QString::number(brush.color().rgba(), 16));
              element.setAttribute("QBrushStyle", brush.style());
              return element;
            }
            
            sierdzioS 1 Reply Last reply
            0
            • T TheTrueGoofy

              @sierdzio

              That's what I had in mind too (as a fallback-scenario). Certainly this is also quite simple to implement, but I was hoping that there is a more elegant solution.

              QDomElement createDomElement(QDomDocument& domDocument,
                                           const QBrush& brush,
                                           const QString& tagName = "QBrush")
              {
                QDomElement element = domDocument.createElement(tagName);
                element.setAttribute("QBrushColorARGB", "#" + QString::number(brush.color().rgba(), 16));
                element.setAttribute("QBrushStyle", brush.style());
                return element;
              }
              
              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @TheTrueGoofy said in Read and write QBrush (QPen, QFont) as XML:

              Certainly this is also quite simple to implement, but I was hoping that there is a more elegant solution.

              I think the only other way is to create feature request for QtGui, or implement a Qt patch yourself - to add a QBrush::toString() method (or QTextStream overload), with a corresponding QBrush::fromString().

              (Z(:^

              1 Reply Last reply
              2

              • Login

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