[Interest] Display XML in tree view

Murphy, Sean smurphy at walbro.com
Wed Aug 3 17:26:32 CEST 2016


> Look at Roles. In your case, the displayRole (role 0, default) is "widget" You
> can add your own roles starting with Qt::UserRole. See roleNames() roles are
> additional data you can attach to the model items.

I'm not sure what roles gets me? Since I'm trying to display a read-only 
representation of the original XML tree structure and just allow the user to 
expand/collapse nodes they don't really care about, I think I'm ONLY concerned
about Qt::DisplayRole, since that's what the QTreeView uses to populate the
widget.

The "hardest" part of what I needed to do, and I use "hardest" very loosely
since it wasn't very difficult, was getting the properly formatted raw XML
string out of the DOM. The issue is there isn't anything in the QDomNode class 
that produces a QString in the format I want:  the raw XML tag (with the possible 
caveat of QDomNode::save(), I'm looking at that now).

So I've modified the DomItem class from the simple DOM model example,
http://doc.qt.io/qt-5/qtwidgets-itemviews-simpledommodel-example.html, 
to have a rawXMLTag() function that recreates that raw XML tag from the internal 
QDomNode. Then in myTreeModel::data() it's trivial to call DomItem::rawXMLTag()
when role == Qt::DisplayRole.

My understanding of the usefulness of Qt::UserRole is if I wanted to pass 
other data around that is visually hidden from the user, but is would still 
be accessible when connecting slots to signals like
  QAbstractItemView::clicked()
But in my case, I don't have any need to pass around other data, I only care 
about showing that original XML tag.

Or am I missing the point of what you're saying?
Sean



More information about the Interest mailing list