[PySide] simple QTableView example

Frank Rueter | OHUfx frank at ohufx.com
Fri Oct 11 14:35:52 CEST 2013


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
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131011/00a8043c/attachment.html>


More information about the PySide mailing list