<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Great, thanks Tibold and Aaron! That all makes sense.<br>
<br>
Incidentally I do have planned a bit more than just the standard
table UI but thought I'd leave that for when I'm more comfortable
with the basic concepts again.<br>
So am keen to test all suggested approaches and have a feeling I
might have to go with a custom solution as I want some animation to
happen when sorting (each line moving to it's new position) - but
that's for later, for now I will stick to the basics.<br>
<br>
Aaron, when trying the setItemDelegateForColumn (sorry, how could I
not have seen this one before) things work fine but I have to cast
the incoming data to int() explicitly in side the setEditorData. The
setModelData method seems to automatically cast the integer back to
a string. Is this the right way to do it?<br>
<br>
def setEditorData(self, spinBox, index):<br>
value = index.model().data(index)<br>
spinBox.setValue(<b>int(value)</b>)<b> # cast string to int</b><br>
<br>
def setModelData(self, spinBox, model, index):<br>
spinBox.interpretText()<br>
value = spinBox.value()<br>
model.setData(index, <b>value</b>)<b> # no need to cast int
back to string?</b><br>
<br>
<br>
Looking ahead: If I want to make the rows/tasks animate to their new
positions upon sorting, can I re-implement the paint methods of,
say, a QAbstractItemView or do I have to go back further and do more
manual work?<br>
I need to stick to the standard PySide package for this for various
reasons.<br>
<br>
Cheers and thanks again Tibold, Aaron and Sebastian, you are a great
help as usual!<br>
<br>
frank<br>
<br>
<br>
<br>
<div class="moz-cite-prefix">On 13/10/13 12:39 AM, Aaron Richiger
wrote:<br>
</div>
<blockquote cite="mid:5259CF8F.9090602@bluewin.ch" type="cite">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<div class="moz-cite-prefix">Hello!<br>
<br>
Tibolds approach would work (and could result in a nicer UI).
Both solutions are possible, some thoughts about the QTableView
approach:<br>
<br>
- your app smells like a future database app. QTableView is in
advantage then, because by using QSqlTableModel, you get all the
mapping betwenn the db and the model for free. <br>
- By using QTableView, sorting, filtering etc. is already
included and if your table has manymany rows, it will be much
faster than any self implemented filtering/sorting algorithm.
But as long, as your todo list doesn't have thousands of entries
(and I hope so for you:-), performance isn't an argument.<br>
<br>
Changes to your code to have the spinbox in the middle column
only:<br>
<br>
priorityDelegate = SpinBoxDelegate(tableView)<br>
tableView.setItemDelegateForColumn(1, priorityDelegate)<br>
<br>
Like this, the first column remains "text-editable", and for the
last column with the checkbox, you don't even necessarily have
to implement a new delegate, reimplementing .flags(), setData()
and data() methods of your model is enough (but if you want a
pure checkbox without a label next to it, you have to write your
own delegate, I could send you the code).<br>
<br>
Feel free to choose what ever way you want, both are perfectly
doable, having advantages where the other variant has
disadvantages...<br>
<br>
Cheers<br>
Aaron<br>
<br>
<br>
Am 13.10.2013 00:06, schrieb Tibold Kandrai:<br>
</div>
<blockquote
cite="mid:5259c8d3.486b0e0a.0d06.ffffc793@mx.google.com"
type="cite">
<style data-externalstyle="true"><!--
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {
margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
}
p.MsoListParagraphCxSpFirst, li.MsoListParagraphCxSpFirst, div.MsoListParagraphCxSpFirst, p.MsoListParagraphCxSpMiddle, li.MsoListParagraphCxSpMiddle, div.MsoListParagraphCxSpMiddle, p.MsoListParagraphCxSpLast, li.MsoListParagraphCxSpLast, div.MsoListParagraphCxSpLast {
margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
line-height:115%;
}
--></style>
<style><!--
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {
margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
}
p.MsoListParagraphCxSpFirst, li.MsoListParagraphCxSpFirst, div.MsoListParagraphCxSpFirst, p.MsoListParagraphCxSpMiddle, li.MsoListParagraphCxSpMiddle, div.MsoListParagraphCxSpMiddle, p.MsoListParagraphCxSpLast, li.MsoListParagraphCxSpLast, div.MsoListParagraphCxSpLast {
margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
line-height:115%;
}
--></style>
<div data-externalstyle="false" dir="ltr"
style="font-family:Calibri,'Segoe UI',Meiryo,'Microsoft YaHei
UI','Microsoft JhengHei UI','Malgun Gothic','Khmer
UI','Nirmala UI',Tunga,'Lao
UI',Ebrima,sans-serif;font-size:12pt;">
<div>If you ask me personally, I wouldn’t use QTableWidget.
Look into QTreeView or QListWidget.</div>
<div>I think they are more suitable for such tasks and are
easier to handle.</div>
<div> </div>
<div>With QTreeView you can use QItemDelegate, to create a
special rendering.</div>
<div>With QListWidget you can simply add a widget per row and
inside the widget you can put whatever.</div>
<div> </div>
<div>ATM I’m in the middle of a 2000 km road trip so I can’t
rally provide you sample's, but if you need help next week
I’m glad to give you samples how to use these widgets.</div>
<div data-signatureblock="true">
<div> </div>
<div>Cheers,</div>
<div>Tibold Kandrai</div>
<div> </div>
</div>
<div style="padding-top: 5px; border-top-color: rgb(229, 229,
229); border-top-width: 1px; border-top-style: solid;">
<div><font style="line-height: 15pt; letter-spacing: 0.02em;
font-family: Calibri, "Segoe UI", Meiryo,
"Microsoft YaHei UI", "Microsoft JhengHei
UI", "Malgun Gothic", "Khmer
UI", "Nirmala UI", Tunga, "Lao
UI", Ebrima, sans-serif; font-size: 11pt;"
face="Calibri, 'Segoe UI', Meiryo, 'Microsoft YaHei UI',
'Microsoft JhengHei UI', 'Malgun Gothic', 'Khmer UI',
'Nirmala UI', Tunga, 'Lao UI', Ebrima, sans-serif"><b>From:</b> Frank
Rueter | OHUfx<br>
<b>Sent:</b> Saturday, 12 October 2013 22:49<br>
<b>To:</b> Tibold Kandrai<br>
<b>Cc:</b> <a moz-do-not-send="true"
class="moz-txt-link-abbreviated"
href="mailto:pyside@qt-project.org">pyside@qt-project.org</a></font></div>
</div>
<div> </div>
Is this the best way to do it though? I.e. having one item per
cell? s there another way at all?<br>
I'm still a bit lost in the model/view design and can't find
the answer online.<br>
<br>
I'm simply trying to have each row represent a "task" with a
title/description (string), a status (boolean) and a priority
(integer). For the integer I need a spin box and for the
boolean I need a checkbox. The examples I found online all
seem to be doing something slightly different and often use
different ways which makes matters more confusing.<br>
<br>
Here is what I have at the moment:<br>
<a moz-do-not-send="true" title="http://pastebin.com/H3GD0xVB"
class="moz-txt-link-freetext"
href="http://pastebin.com/H3GD0xVB" target="_parent">http://pastebin.com/H3GD0xVB</a><br>
<br>
The "status" and "priority" values don't display currnelty as
I haven't figured out how to properly assign a delegate to
just those cells. At the top I tried to define a n item
delegete for a spin box but I'm not sure how to properly
assign it.<br>
<br>
Do I have to make the delegate draw different widgets (spin
box / checkbox) depending on data type, or can/should I use a
different delegate for each cell?<br>
<br>
I'm sure the answer is right in front of me, could you please
help one more time please?!<br>
<br>
Cheers,<br>
frank<br>
<br>
<br>
<div class="moz-cite-prefix">On 11/10/13 4:00 PM, Tibold
Kandrai wrote:<br>
</div>
<blockquote style="margin-top: 0px; margin-bottom: 0px;"
cite="mid:848951203488568137@unknownmsgid">
<div>
<div style="font-family: Calibri,sans-serif; font-size:
11pt;">If you mean to use a QStandardItem per cell then
yes.<br>
Also for storing values that you want to display, use
the Qt.DisplayRole as role.<br>
<br>
Cheers,<br>
Tibold Kandrai</div>
</div>
<div dir="ltr">
<hr> <span style="font-family: Calibri,sans-serif;
font-size: 11pt; font-weight: bold;">From: </span><span
style="font-family: Calibri,sans-serif; font-size:
11pt;"><a moz-do-not-send="true"
title="mailto:frank@ohufx.com"
href="mailto:frank@ohufx.com" target="_parent">Frank
Rueter | OHUfx</a></span><br>
<span style="font-family: Calibri,sans-serif; font-size:
11pt; font-weight: bold;">Sent: </span><span
style="font-family: Calibri,sans-serif; font-size:
11pt;">11/10/2013 14:35</span><br>
<span style="font-family: Calibri,sans-serif; font-size:
11pt; font-weight: bold;">To: </span><span
style="font-family: Calibri,sans-serif; font-size:
11pt;"><a moz-do-not-send="true"
title="mailto:kandraitibold@gmail.com"
href="mailto:kandraitibold@gmail.com" target="_parent">Tibold
Kandrai</a></span><br>
<span style="font-family: Calibri,sans-serif; font-size:
11pt; font-weight: bold;">Cc: </span><span
style="font-family: Calibri,sans-serif; font-size:
11pt;"><a moz-do-not-send="true"
title="mailto:pyside@qt-project.org"
href="mailto:pyside@qt-project.org" target="_parent">pyside@qt-project.org</a></span><br>
<span style="font-family: Calibri,sans-serif; font-size:
11pt; font-weight: bold;">Subject: </span><span
style="font-family: Calibri,sans-serif; font-size:
11pt;">Re: [PySide] simple QTableView example</span><br>
<br>
</div>
one more silly question if I may:<br>
So if I have a task like this:<br>
newTask = {'title':'new task', 'priority':1,
'status':False}<br>
<br>
and need to store the data in one row in the model I should
use three different items, one for each value, right?!<br>
<br>
e.g.:<br>
<br>
newTask = {'title':'new task', 'priority':1,
'status':False}<br>
row = self.model.rowCount()<br>
for column, attr in enumerate(['title', 'priority',
'status']):<br>
newItem = QtGui.QStandardItem(newTask[attr])<br>
self.model.setItem(row, column, newItem)<br>
<br>
then juggle delegates or widgets to use a spin box for the
integer and a checkbox for the boolean...<br>
<br>
Thanks for the help!<br>
<br>
Cheers,<br>
frank<br>
<br>
<div class="moz-cite-prefix">On 10/10/13 11:44 PM, Tibold
Kandrai wrote:<br>
</div>
<blockquote style="margin-top: 0px; margin-bottom: 0px;"
cite="mid:52572219.82eccc0a.55f2.ffff8462@mx.google.com">
<div dir="ltr">
<div>Hey,</div>
<div> </div>
<div>I’m not sure I understand the problem correctly.</div>
<div> </div>
<div>If you want to store data in a cell or a
QStandardItem, then you need to use setData() and
data().</div>
<div>Generally you shouldn’t need to subclass
QStandardItem or QStandardItemModel.</div>
<div>Here is an example how:</div>
<div> </div>
<div># Define roles</div>
<div><font style="font-size: 10pt;" face="Consolas">FINISHED</font><font
style="font-size: 10pt;" face="Consolas">_ROLE =
QtCore.Qt.UserRole + 1</font><br>
<font style="font-size: 10pt;" face="Consolas">PRIORITY</font><font
style="font-size: 10pt;" face="Consolas">_ROLE =
QtCore.Qt.UserRole + 2</font><br>
</div>
<div><font style="font-size: 10pt;" face="Consolas">#
Create model</font></div>
<div><font style="font-size: 10pt;" face="Consolas">model
= QtGui.QStandardItemModel()</font></div>
<div><font style="font-size: 10pt;" face="Consolas">item
= QtGui.QStandarItem()</font></div>
<div><font style="font-size: 10pt;" face="Consolas">model.appendRow(item)</font></div>
<div><font style="font-size: 10pt;" face="Consolas">item_index
= item.index()</font></div>
<div> </div>
<div><font style="font-size: 10pt;" face="Consolas">#
Store data using the item</font></div>
<div><font style="font-size: 10pt;" face="Consolas">item.setData(finished,
FINISHED_ROLE)</font></div>
<div><font style="font-size: 10pt;" face="Consolas">item.setData(priority</font><font
style="font-size: 10pt;" face="Consolas">,
PRIORITY_ROLE)</font></div>
<div> </div>
<div>
<div><font style="font-size: 10pt;" face="Consolas">#
Store data using the model</font></div>
<div><font style="font-size: 10pt;" face="Consolas">model</font><font
style="font-size: 10pt;" face="Consolas">.setData(item_index,
finished, FINISHED_ROLE)</font></div>
<div><font style="font-size: 10pt;" face="Consolas">model.</font><font
style="font-size: 10pt;" face="Consolas">setData(item_index,
priority, PRIORITY_ROLE)</font></div>
</div>
<div> </div>
<div><font style="font-size: 10pt;" face="Consolas">#
Retrieve data using the item</font></div>
<div><font style="font-size: 10pt;" face="Consolas">finished
= </font><font style="font-size: 10pt;"
face="Consolas">item.data(FINISHED_ROLE)</font></div>
<div><font style="font-size: 10pt;" face="Consolas">priority
= </font><font style="font-size: 10pt;"
face="Consolas">item.data(PRIORITY_ROLE)</font></div>
<div> </div>
<div><font style="font-size: 10pt;" face="Consolas">#
Retrieve data using the model</font></div>
<div>
<div><font style="font-size: 10pt;" face="Consolas">finished
= model.data(item_index, FINISHED_ROLE)</font></div>
<div><font face="Cambria"><font style="font-size:
10pt;" face="Consolas">priority =
model.data(item_index, PRIORITY_ROLE)</font></font></div>
</div>
<div>
<div> </div>
<div>In some cases like click event handlers, you have
the model and the item index, there it’s easier to
use the model methods instead of finding the item
and then getting the data. <span><span
style="font-family: "Segoe UI
Symbol","Apple Color Emoji";"
data-externalstyle="false">�</span></span></div>
<div> </div>
<div>Hope it helps.</div>
<div> </div>
<div>Cheers,</div>
<div>Tibold</div>
<div> </div>
</div>
<div style="padding-top: 5px; border-top-color: rgb(229,
229, 229); border-top-width: 1px; border-top-style:
solid;">
<div><font face="Calibri, 'Segoe UI', Meiryo,
'Microsoft YaHei UI', 'Microsoft JhengHei UI',
'Malgun Gothic', 'Khmer UI', 'Nirmala UI', Tunga,
'Lao UI', Ebrima, sans-serif"><b>From:</b> Frank
Rueter | OHUfx<br>
<b>Sent:</b> 2013 October 10, Thursday
19:37<br>
<b>To:</b> <a moz-do-not-send="true"
title="mailto:pyside@qt-project.org"
class="moz-txt-link-abbreviated"
href="mailto:pyside@qt-project.org"
target="_parent">pyside@qt-project.org</a></font></div>
</div>
<div> </div>
After looking at some more examples I think my approach
of storing multiple values in one item is fundamentally
flawed.<br>
Instead I should be using one item per cell and assign
the respective data, right?!<br>
<br>
I shall re-write the example accordingly, sorry for the
noise.<br>
<br>
frank<br>
<br>
<div class="moz-cite-prefix">On 10/10/13 6:34 PM, Frank
Rueter | OHUfx wrote:<br>
</div>
<blockquote style="margin-top: 0px; margin-bottom: 0px;"
cite="mid:5256D715.5020301@ohufx.com"> I meant
QTableView not QStandardTableView :/<br>
<br>
<div class="moz-cite-prefix">On 10/10/13 6:33 PM,
Frank Rueter | OHUfx wrote:<br>
</div>
<blockquote style="margin-top: 0px; margin-bottom:
0px;" cite="mid:5256D6C1.70508@ohufx.com"> Hi all,<br>
<br>
after a bit of a break from PySide I am trying to
wrap my head around the model/view stuff again and
am trying to understand how a very simple example
would work where a QStandarItem has properties
"title", "priority" and "finished" which are
displayed via a QStandardTableView.<br>
<br>
I am struggling with understanding how to properly
display the above three properties in the table's
columns. I tried setting the data() method on the
model like this:<br>
<br>
<i> def data(self, index,
role=QtCore.Qt.DisplayRole):</i><i><br>
</i><i> '''Return data based on index and
role'''</i><i><br>
</i><i> item = self.itemFromIndex(index)</i><i><br>
</i><i> if index.column() == 0:</i><i><br>
</i><i> return item.title</i><i><br>
</i><i> elif index.column() == 1:</i><i><br>
</i><i> return item.finished</i><i><br>
</i><i> elif index.column() == 2:</i><i><br>
</i><i> return item.priority</i><br>
<br>
but for some reason it errors saying item does not
have attribute "finished" even though my item object
s declared like this:<br>
<br>
<i>class TaskItem(QtGui.QStandardItem):</i><i><br>
</i><i> '''Item to hold a task for the todo
list'''</i><i><br>
</i><i> </i><i><br>
</i><i> def __init__(self, title, finished=False,
priority=1):</i><i><br>
</i><i> super(TaskItem, self).__init__(title)</i><i><br>
</i><i> self.title = title</i><i><br>
</i><i> self.finished = finished</i><i><br>
</i><i> self.priority = priority</i><br>
<br>
<br>
When printing the item's attributes via dir() I see
that, when the model is populated, the last item it
attempts to call is not my custom item object, but
something else with less attributes and methods.
Clearly there is something I haven't quite
understood about this process.<br>
<br>
Also, if I use the models data() method as pointed
out above, I get checkboxes in the cells which I
don't want at this stage.<br>
<br>
Can somebody please help me understand where I go
wrong?<br>
Attached is the whole test code.<br>
<br>
Cheers,<br>
frank<br>
<br>
P.S.: I am aware that the controller code shouldn't
necessarily live in the QWidget's methods, this is
just for testing which I will clean up once I get
how it all connects again<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre>_______________________________________________
PySide mailing list
<a moz-do-not-send="true" title="mailto:PySide@qt-project.org" class="moz-txt-link-abbreviated" href="mailto:PySide@qt-project.org" target="_parent">PySide@qt-project.org</a>
<a moz-do-not-send="true" title="http://lists.qt-project.org/mailman/listinfo/pyside" class="moz-txt-link-freetext" href="http://lists.qt-project.org/mailman/listinfo/pyside" target="_parent">http://lists.qt-project.org/mailman/listinfo/pyside</a>
</pre>
</blockquote>
<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre>_______________________________________________
PySide mailing list
<a moz-do-not-send="true" title="mailto:PySide@qt-project.org" class="moz-txt-link-abbreviated" href="mailto:PySide@qt-project.org" target="_parent">PySide@qt-project.org</a>
<a moz-do-not-send="true" title="http://lists.qt-project.org/mailman/listinfo/pyside" class="moz-txt-link-freetext" href="http://lists.qt-project.org/mailman/listinfo/pyside" target="_parent">http://lists.qt-project.org/mailman/listinfo/pyside</a>
</pre>
</blockquote>
<br>
</div>
</blockquote>
<br>
</blockquote>
<br>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
PySide mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:PySide@qt-project.org">PySide@qt-project.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://lists.qt-project.org/mailman/listinfo/pyside">http://lists.qt-project.org/mailman/listinfo/pyside</a>
</pre>
</blockquote>
<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
PySide mailing list
<a class="moz-txt-link-abbreviated" href="mailto:PySide@qt-project.org">PySide@qt-project.org</a>
<a class="moz-txt-link-freetext" href="http://lists.qt-project.org/mailman/listinfo/pyside">http://lists.qt-project.org/mailman/listinfo/pyside</a>
</pre>
</blockquote>
<br>
</body>
</html>