[PySide] simple QTableView example

Sebastian Elsner sebastian at risefx.com
Sun Oct 13 00:43:02 CEST 2013


Hi all,

the approach I like most is described in this blog entry:
http://www.saltycrane.com/blog/2007/06/pyqt-42-qabstracttablemodelqtableview/

I think this is what you want.

Cheers

Sebastian


Am 12.10.2013 22:49, schrieb Frank Rueter | OHUfx:
> Is this the best way to do it though? I.e. having one item per cell? s
> there another way at all?
> I'm still a bit lost in the model/view design and can't find the
> answer online.
>
> 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.
>
> Here is what I have at the moment:
> http://pastebin.com/H3GD0xVB
>
> 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.
>
> 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?
>
> I'm sure the answer is right in front of me, could you please help one
> more time please?!
>
> Cheers,
> frank
>
>
> On 11/10/13 4:00 PM, Tibold Kandrai wrote:
>> If you mean to use a QStandardItem per cell then yes.
>> Also for storing values that you want to display, use the
>> Qt.DisplayRole as role.
>>
>> Cheers,
>> Tibold Kandrai
>> ------------------------------------------------------------------------
>> From: Frank Rueter | OHUfx <mailto:frank at ohufx.com>
>> Sent: ?11/?10/?2013 14:35
>> To: Tibold Kandrai <mailto:kandraitibold at gmail.com>
>> Cc: pyside at qt-project.org <mailto:pyside at qt-project.org>
>> Subject: Re: [PySide] simple QTableView example
>>
>> one more silly question if I may:
>> So if I have a task like this:
>>         newTask = {'title':'new task', 'priority':1, 'status':False}
>>
>> and need to store the data in one row in the model I should use three
>> different items, one for each value, right?!
>>
>> e.g.:
>>
>>         newTask = {'title':'new task', 'priority':1, 'status':False}
>>         row = self.model.rowCount()
>>         for column, attr in enumerate(['title', 'priority', 'status']):
>>             newItem = QtGui.QStandardItem(newTask[attr])
>>             self.model.setItem(row, column, newItem)
>>
>> then juggle delegates or widgets to use a spin box for the integer
>> and a checkbox for the boolean...
>>
>> Thanks for the help!
>>
>> Cheers,
>> frank
>>
>> On 10/10/13 11:44 PM, Tibold Kandrai wrote:
>>> Hey,
>>> I'm not sure I understand the problem correctly.
>>> If you want to store data in a cell or a QStandardItem, then you
>>> need to use setData() and data().
>>> Generally you shouldn't need to subclass QStandardItem or
>>> QStandardItemModel.
>>> Here is an example how:
>>> # Define roles
>>> FINISHED_ROLE = QtCore.Qt.UserRole + 1
>>> PRIORITY_ROLE = QtCore.Qt.UserRole + 2
>>> # Create model
>>> model = QtGui.QStandardItemModel()
>>> item = QtGui.QStandarItem()
>>> model.appendRow(item)
>>> item_index = item.index()
>>> # Store data using the item
>>> item.setData(finished, FINISHED_ROLE)
>>> item.setData(priority, PRIORITY_ROLE)
>>> # Store data using the model
>>> model.setData(item_index, finished, FINISHED_ROLE)
>>> model.setData(item_index, priority, PRIORITY_ROLE)
>>> # Retrieve data using the item
>>> finished = item.data(FINISHED_ROLE)
>>> priority = item.data(PRIORITY_ROLE)
>>> # Retrieve data using the model
>>> finished = model.data(item_index, FINISHED_ROLE)
>>> priority = model.data(item_index, PRIORITY_ROLE)
>>> 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. ?
>>> Hope it helps.
>>> Cheers,
>>> Tibold
>>> *From:* Frank Rueter | OHUfx
>>> *Sent:* ?2013? ?October? ?10?, ?Thursday ?19?:?37
>>> *To:* pyside at qt-project.org
>>> After looking at some more examples I think my approach of storing
>>> multiple values in one item is fundamentally flawed.
>>> Instead I should be using one item per cell and assign the
>>> respective data, right?!
>>>
>>> I shall re-write the example accordingly, sorry for the noise.
>>>
>>> frank
>>>
>>> On 10/10/13 6:34 PM, Frank Rueter | OHUfx wrote:
>>>
>>>     I meant QTableView not QStandardTableView :/
>>>
>>>     On 10/10/13 6:33 PM, Frank Rueter | OHUfx wrote:
>>>
>>>         Hi all,
>>>
>>>         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.
>>>
>>>         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:
>>>
>>>         /    def data(self, index, role=QtCore.Qt.DisplayRole)://
>>>         //        '''Return data based on index and role'''//
>>>         //        item = self.itemFromIndex(index)//
>>>         //        if index.column() == 0://
>>>         //            return item.title//
>>>         //        elif index.column() == 1://
>>>         //            return item.finished//
>>>         //        elif index.column() == 2://
>>>         //            return item.priority/
>>>
>>>         but for some reason it errors saying item does not have
>>>         attribute "finished" even though my item object s declared
>>>         like this:
>>>
>>>         /class TaskItem(QtGui.QStandardItem)://
>>>         //    '''Item to hold a task for the todo list'''//
>>>         ////
>>>         //    def __init__(self, title, finished=False, priority=1)://
>>>         //        super(TaskItem, self).__init__(title)//
>>>         //        self.title = title//
>>>         //        self.finished = finished//
>>>         //        self.priority = priority/
>>>
>>>
>>>         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.
>>>
>>>         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.
>>>
>>>         Can somebody please help me understand where I go wrong?
>>>         Attached is the whole test code.
>>>
>>>         Cheers,
>>>         frank
>>>
>>>         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
>>>
>>>
>>>         _______________________________________________
>>>         PySide mailing list
>>>         PySide at qt-project.org
>>>         http://lists.qt-project.org/mailman/listinfo/pyside
>>>
>>>
>>>
>>>
>>>     _______________________________________________
>>>     PySide mailing list
>>>     PySide at qt-project.org
>>>     http://lists.qt-project.org/mailman/listinfo/pyside
>>>
>>>
>>
>
>
>
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131013/5cc2f516/attachment.html>


More information about the PySide mailing list