Hi,<br><br><div class="gmail_quote">On Mon, Jul 9, 2012 at 12:02 AM, Diego <span dir="ltr"><<a href="mailto:diego.no.spam@gmail.com" target="_blank">diego.no.spam@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I have a QTreeView widget that displays one Message per row. It also<br>
has several columns, each of which displays one Message's attribute.<br>
The tree's model is a QStandardItemModel that I populate this way:<br>
<br>
for m in messages:<br>
items = []<br>
it = QStandardItem()<br>
it.message = m # (1) See here<br>
it.setData(m.headers['Subject'], Qt.DisplayRole)<br>
it.setCheckable(False)<br>
items.append(it)<br>
<br>
it = QStandardItem()<br>
it.message = m # (2) See here<br>
it.setData(m.headers['From'], Qt.DisplayRole)<br>
items.append(it)<br>
<br>
it = QStandardItem()<br>
it.message = m # (3) See here<br>
it.setData(m.headers['Date'], Qt.DisplayRole)<br>
items.append(it)<br>
<br>
tree_view.model().appendRow(items)<br>
<br>
<br>
When a row is selected in the QTreeView, I need to be able to access<br>
the object whose attributes are being displayed in that row. I don't<br>
know how to properly do that, and the only thing that worked for me is<br>
to store the original object in each and every item I create, as in<br>
(1), (2) and (3) in the code snippet above. It appears to me as a very<br>
inefficient way of solving this problem and I think there must be a<br>
much better solution for it.<br>
<br>
So my question is: how do I store the original Message object in every<br>
row in order to get access to it when the user selects that row?</blockquote><div><br></div><div>You could store it only in the first item of a row and always access it there. Another option would be have a dict of messages and use the row-number as key - this only works of course as long as your rows are not re-arranged frequently.</div>
<div><br></div><div>Both approaches however duplicate some of the data though - everything thats displayed to the user. To solve that you'd have to write your own model subclass which directly works on the messages and returns the corresponding data from the data() function. With such a model you could also return the original message object when data() is called with a custom data role.</div>
<div><br></div><div>Andreas</div></div><br>