PDA

View Full Version : Questions on Model/View



joshuajcarson
23rd September 2008, 21:09
This is less of a question on Model/View, and much more of a question on if I'm actually understanding it correctly.

Using a QTreeView, QStandardItemModel, and QStandardItems, I can create a tree of a bunch of text. If I want to be able to display html, my best bet is likely to subclass QStandardItem, set that as the delegate, and implement the paint function (most likely putting the String into a QTextDocument and drawing that). If I want to be able to actually click on the links that are shown, I will have to handle context menu events when users click on the QTreeView.

Does this sound about right? I mostly just want to do what's here http://doc.trolltech.com/4.4/itemviews-simpletreemodel.html but be able to display html tables and links.

Kal
24th September 2008, 02:34
I am also struggling a bit with the whole model/view system but I think I can help by telling you that you are pretty much correct. The only difference is that delegates are different than standardItems. You may not even need to subclass qStandardItem because it will hold displayRole data already. Your delegate subclass, like you said, will override the paint method and look for those displayRoles and choose to draw them in a textDocument instead of ..whatever it normally uses.

caduel
24th September 2008, 08:15
For html display you will need to provide a delegate (e.g. a subclass of QItemDelegate; ovewrite its paint method).
For handling clicks (with the left mouse) button, you do not need to overwrite context menu events, but rather mouseReleaseEvent.) The only issue here being that you need to find out whether there is a link under your mouse cursor. For that (and for painting html) you might want to take a look at QAbstractTextDocumentLayout, QTextDocument and search the forums. There are some helpful threads on this topic.)

joshuajcarson
24th September 2008, 19:54
In the situation of a mouse release, it's the view that recieves that signal, correct? Then I would have to check to see what index the mouse was released over? From there, check the data of the index, make sure that data is a link to a website, and open the link? The model and the delegate don't care at all what the user does with their mice, do they?

caduel
24th September 2008, 22:26
>In the situation of a mouse release, it's the view that recieves that signal, correct?
Not a signal, you have to subclass the view and overwrite mouseReleasedEvent (a normal virtual function)


> Then I would have to check to see what index the mouse was released over? From there, check the data of the index, make sure that data is a link to a website, and open the link?
Yes.
If the data is just the link, you can hook to the clicked() signal.
If the data is html that contains an embedded link you need the pixel position of the click to determine whether a (and which) link is under the mouse.

> The model and the delegate don't care at all what the user does with their mice, do they?
Yes.

wysota
26th September 2008, 10:19
>In the situation of a mouse release, it's the view that recieves that signal, correct?
Not a signal, you have to subclass the view and overwrite mouseReleasedEvent (a normal virtual function)
You don't need to touch the view. The delegate receives mouse events for the items in its editorEvent() method.



> The model and the delegate don't care at all what the user does with their mice, do they?
Yes.

The delegate may care.

joshuajcarson
26th September 2008, 20:04
You don't need to touch the view. The delegate receives mouse events for the items in its editorEvent() method.

This will allow me quick and easy access to the mouse events, but isn't this suppose to be used for editing? I'm not trying to edit anything at all. I'm sure it would work, I'm just wondering if this would not quite be following best practices.

wysota
26th September 2008, 22:29
This will allow me quick and easy access to the mouse events, but isn't this suppose to be used for editing?
No, the method name was just chosen badly. You may safely use it.