Read and write QBrush (QPen, QFont) as XML

  • 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 << 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?

  • Moderators

    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"));

  • 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";

  • Moderators

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

  • @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));
      return element;

  • Moderators

    @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().

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.