Hi,
I had thought of the answer you told. And the problem is xml highlighting !
If I simply use treewidget, can I set xml highlighting on it ? I guess I need to use delegates for that, isnt it ?
Or is there some easy way out![]()
Hi,
I had thought of the answer you told. And the problem is xml highlighting !
If I simply use treewidget, can I set xml highlighting on it ? I guess I need to use delegates for that, isnt it ?
Or is there some easy way out![]()
One things that will probably work (not sure if it's the best solution) would indeed be to set a delegate on the QTreeView. No need to subclass it.
Then override the delegates paint method.
create a QTextDocument, a QSyntaxHighlighter for it and set the (fragment for the QModelIndex of the) XML as plain text (in the QTextDocument).
Will probably work. (I did something like that once.)
Perhaps not efficient, though... best give it a try (and tell us about it!)
HTH
I made the treeview of xml subclassing treewidget and treewidgetitem.
Now I can extract path of the node relative to the root element. Also i can change font of the tree widget item.
But still havent started code for delegate.
Before I start it, I haev a question, are delegates called on double click ?? If so it wont help me. I want to render the xml formating on that start itself.
Also my code is working quite slow, I need to check whats wrong, and am worried how slow it will become even if i set delegate![]()
The delegate does
* painting of all cells (this has nothing to to with mouse clicks)
* and the delegate is also responsible for creating editors (if the model is editable)
Use valgrind or something like it to profile your application.
How then in Spin box delegate example, do we have to double click to see the spin box ??painting of all cells (this has nothing to to with mouse clicks)
or is it because of the createEditor function ?? Can anyone explain ?
the spinBox has nothing to do with drawing; it is (as you guessed) the editor created by createEditor.
The cells that are not edited do not show a spin box!
paint: looks for a cell that is not being edited
createEditor: the editor (which often is laid over the edited cell, giving the impression of a different painting while being edited. it is just an impression. the cell is always painted by paint.)
HTH
Well, started reading Model/View architecture in Qt.. but I wanted some immediate results.
So I tried the following -
copied the simple DOM example, and modified it that I show attributes in column 0 along with tag name. So something like an XML Tree. Now moving on with delegates, made a little progress.
I overrided the drawDisplay method of QItemDelegate -
Qt Code:
void XMLDelegate::drawDisplay ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text ) const { //QItemDelegate::drawDisplay(painter,option,rect,text); int len = textToDisplay.length(); int x,y; x = rect.x(); y = rect.y()+10; for(int i=0;i<len;i++) { painter->setPen(colors[i%3]); x += option.fontMetrics.width(textToDisplay[i]); } }To copy to clipboard, switch view to plain text mode
By this am able to display text in alternate colors. Now thing is,,, I havent handled it for Wrap mode. And without that one display wont look good ...
Isnt there some simple way to do this ? Currently to display xml, we use Internet explorer and Navigate function for a xml file. It does display the xml with highlighting. But I want to add context menu to sucha tree, and be able to compute xpath of the node clicked...
Can someone suggest a simpler way ?? Or do I have to do it the hard way![]()
Could you provide a screenshot or a mockup of what you want? I think your problem is easy to solve, but first I need to make sure what you want![]()
Even I think the problem must be easy to solve, may be am moving in wrong direction , may be not
Well, I will restate my problem with snapshots and also give an idea as what I am able to achieve till now.
First, the problem :
I need to display xml data as it displays in internet explorer. However, additionaly I want to be able to show a context menu and be able to extract the Xpath of the node that was right clicked.
Refer : required functionality.jpg
I know I can achieve this by -
1) Make a tree out of xml
2) apply syntax highlighter to each row
3) Handle multi line
But I dont know whats the best solution to achieve above steps
Now the things I have tried till now :
1) I subclassed QTreeWidget and QTreeWidgetItem and made a Tree from XML Data. This works fine, shows the data like in explorer. I override drawBranches() of treewidget.
But problem is, I can show it only in one Color.
2) I modified the Simple DOM Model example, and returned text in column 0 only instead of Column 1 and 2. I also made a custom delegate where I overrided drawDisplay() and was able to show text in different colors. Problem with this solution is, I see row text in single line only, and the text is clipped if its longer than the rect width.
Refer try 2.jpg
Now how do I handle multiple line in a single cell ?? Like in iexplorer, if we resize the window, the xml data gets wrapped properly. How do I achieve this functionality ??![]()
assuming you do the drawing with QTextDocument; use setTextWidth, pass the size of the rectangle passed to QAbstractItemDelegate:aint (in QStyleOptionViewItem)
HTH
Dont assume !!
Am not using QTextDocument. If only I cud get QtextDocument for a model index, i cud have set the syntax highlighter for it.
What I am doing is setting a ItemDelegate for the Tree View in Simple DOM Model example.
am ONLY overriding ItemDelegate::drawDisplay() function. The code is same as posted few posts back.
Now say I have text = "Hello, How are you";
If i need to display each word in different color, I need to first write Hello with one color, then get the width occupied by Hello, and write How with other color and so on. I just cant simply write the whole text in one goCan I ??
Also see drawDisplay() in QItemDelegate.cpp. There are many things to consider, which are going above my head for the time being
Also I made a small parser inside the drawDisplay() function, and am able to get xml like highlighting
But prob remains... single linedoesnt gets wrapped..
Another prob is, values are treated as child nodes, So if i want to display text vlaue of a node like <node> nodeValue </node> , am not able to do it. I will have to look in model how to prevent adding row for #text nodes![]()
ok, how about
i) using a QItemDelegate in the QTreeWidget approach, too?
ii) pass Qt::TextWordWrap to QPainter::drawText
in YourItemDelegate:aint(...):
I would not do the algorithm for drawing colored text myself (unless I had to, that is).
Doesn't a simple
work for you?Qt Code:
painter->translate(option.rect.topLeft()); QTextDocument doc; // if you want wrapping text // doc.setTextWidth(option.rect.width()); doc.setHtml(text); doc.drawContents(painter);To copy to clipboard, switch view to plain text mode
(Maybe it is inefficient, but it is easy to implement. And maybe it is fast enough, too.
aamer4yu (14th July 2008)
I'm not sure if a tree is the best way to visualize the document this way, but since you already have most things done, let's keep it this way.
Drawing wrapped text is easy - use a variant of QPainter::drawText() that takes flags as one of its parameters and pass it a flag to wrap text. But... this way you won't be able to highlight syntax...
Another way is to use QTextLayout and QTextLine which is quite straightforward, but... no highlighting either.
Probably the best way would be to have a QTextDocument that the delegate would use to render the xml. You would apply a piece of text as the document, trigger the highlighter, render the document, apply a new piece of text, etc. Just make sure not to create a new document in each pass of the delegate - reuse the old one instead.
Bookmarks