Results 1 to 20 of 30

Thread: XML, Highlighting and Model/View

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default XML, Highlighting and Model/View

    Hi,
    I have my data in QDomElement / QDomDocument form, and I simply display it using a web browser element.On clicking, the node expands/contracts as in internet explorer. Fine.

    Problem :
    Now, I have a situation where I want to right click on a xml node and be able to tell its path from the root element. say if i clicked D, I should get "Root/A/B/C/D" . Also I want to keep the xml highlighting.
    I have two solutions in mind -
    1) Implement my own item model and item view. Simplest would be using QStandardItem and QTreeView I guess. But problem with this solution is,,, I havent used model/view till now and it looks very comples to me. Also how do I maintain XML highting even if I manage to make a tree view out of my xml data ?

    2) Inherit QTreeWidget and QTreeWidgetItem. and set a delegate for it. The delegate is supposed to do the XML highlighing. Also what functions wud I need to override ? I havent worked with delegates too till now

    Which one of the above 2 is better ?
    Is there any other way I could achieve my goal more easily and efficiently ?? I dont wanna increase my code.

    Any ideas ???

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: XML, Highlighting and Model/View

    To i)
    Learning model/view is worth the trouble.
    I am not saying you should for this problem; but take a look at it anyway!

    ii) I don't see why you would need/want to subclass QTreeWidget (if at all: QTreeView, probably?)? As long as you do not have a model, a view (or delegate) won't help you.


    There is an example of adapting XML to a model in Qt4's examples (look for "Simple DOM Model"). Take a look at it.
    Then put that in a QTreeView.
    Connect to the contextMenu event (or something like), translate the mouse pos. to the QModelIndex, get the DomNode from that, and from that, the path.

    HTH

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    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

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: XML, Highlighting and Model/View

    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

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    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

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: XML, Highlighting and Model/View

    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.

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    painting of all cells (this has nothing to to with mouse clicks)
    How then in Spin box delegate example, do we have to double click to see the spin box ??
    or is it because of the createEditor function ?? Can anyone explain ?

  8. #8
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: XML, Highlighting and Model/View

    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

  9. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    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:
    1. void XMLDelegate::drawDisplay ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text ) const
    2. {
    3. //QItemDelegate::drawDisplay(painter,option,rect,text);
    4.  
    5. QColor colors[3] = {Qt::black,Qt::red,Qt::blue};
    6. QColor red(Qt::red);
    7. QColor blue(Qt::blue);
    8. QColor black(Qt::black);
    9.  
    10. QString textToDisplay = option.fontMetrics.elidedText(text,Qt::ElideNone,rect.width());
    11. int len = textToDisplay.length();
    12. int x,y;
    13. x = rect.x();
    14. y = rect.y()+10;
    15. for(int i=0;i<len;i++)
    16. {
    17. painter->setPen(colors[i%3]);
    18. painter->drawText(x,y,QString(textToDisplay[i]));
    19. x += option.fontMetrics.width(textToDisplay[i]);
    20. }
    21. }
    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML, Highlighting and Model/View

    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

  11. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    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 ??
    Attached Images Attached Images

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.