[Interest] Display XML in tree view

Jason H jhihn at gmx.com
Wed Aug 3 16:38:52 CEST 2016



> Sent: Wednesday, August 03, 2016 at 10:29 AM
> From: "Murphy, Sean" <smurphy at walbro.com>
> To: "interest at qt-project.org" <Interest at qt-project.org>
> Subject: Re: [Interest] Display XML in tree view
>
> > > I'm assuming this solution already exists, but either my google-fu isn't
> > working today, or it doesn't: I'd like to display an XML in a tree view, much
> > like many XML editing software applications do. All I'm looking for is display
> > the raw XML in a read-only tree view, but have the nodes be hierarchical so
> > that while looking at the XML, I can collapse/expand nodes.
> > >
> > > I have to believe this is already out there, but I'm just not stumbling across
> > it...
> > 
> > I don't think anything exists already. TreeViews sure, XML sure. Combining
> > the two though, no.  But I don't think it would be hard. Qt's model/view
> > system should make it trivial. The only decisions are:
> > 1. Stream Reader interface or classic DOM parsing (both supported by Qt)
> > 2. Arrange the data as columns or roles.
> > 
> > Once you have your model and parsing, then you can use it in a QTreeView.
> 
> Yeah, I wasn't able to find what I wanted exactly, but the DOM model example,
> http://doc.qt.io/qt-5/qtwidgets-itemviews-simpledommodel-example.html, was
> really close to what I wanted. I just changed it to having only one column and then
> modifying what was written out. The DOM structure was great for generating the
> tree relationships between nodes. 
> 
> My only complaint is once it's in the DOM the raw XML tag is lost, which is all I 
> want to display. Again, correct me if I'm just missing something obvious, I'm 
> pretty new to DOM. But if the original XML tag was:
>   <widget class="QWidget" name="centralWidget">
> that's exactly what I'd want to display, just with the expand/collapse arrow next
> to it. I'm not trying to actually parse and understand the data in the XML, I just want
> to display the XML node structure in the UI. But it seems that DOM is a little too 
> aggressive for my needs when parsing, and breaks up that tag into:
>   QDomNode::nodeName() // which just returns "widget"
>   QDomNode::attributes() // returns a map of attributes that has to be parsed
> 
> Obviously it wasn't too hard to build up a string that puts things back the way they
> were, but I was surprised that doesn't exist already. Or maybe I need to dig a little 
> more? I just stumbled across http://doc.qt.io/qt-5/qdomnode.html#save which
> might do exactly what I need. Off to try to it now...


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.




More information about the Interest mailing list