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
from PySide import QtGui
from PySide import QtCore

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

class ToDoModel(QtGui.QStandardItemModel):
    '''Model for tasks to manage the todo list'''
    
    def __init__(self, parent=None):
        super(ToDoModel, self).__init__(parent)
        self.setColumnCount(3)
        
    def addTask(self, taskItem):
        taskCount = self.rowCount()
        self.setItem(taskCount, 0, taskItem)
    
    def data(self, index, role=QtCore.Qt.DisplayRole):
        '''Return data based on index and role'''
        item = self.itemFromIndex(index)
        print 'items attributes are:', dir(item)
        if index.column() == 0:
            return item.title
        elif index.column() == 1:
            return item.finished
        elif index.column() == 2:
            return item.priority
    
class ToDoProxyModel(QtGui.QSortFilterProxyModel):
    '''Proxy Model for filtering and sorting - not yet in use'''
    
    def __init__(self, parent=None):
        super(ToDoProxyModel, self).__init__(parent)

class ToDoPanel(QtGui.QWidget):
    '''Panel to create and manage a to-do list'''
    def __init__(self, parent=None):
        super(ToDoPanel, self).__init__(parent)
        self.model = ToDoModel()
        self.buildUI()
        self.connectSignalsWithSlots()
        # ADD THE FIRST TASK
        #self.addTask()

    def buildUI(self):
        ## LAYOUT
        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)
        
        ## TOOL BAR
        toolLayout = QtGui.QHBoxLayout()
        self.addButton = QtGui.QPushButton('add task')
        toolLayout.addWidget(self.addButton)
        
        ## TABLE VIEW
        tableView = QtGui.QTableView()
        tableView.setModel(self.model)
        tableView.setSortingEnabled(True)
        tableView.horizontalHeader().setStretchLastSection(True)

        ## CREATE UI
        layout.addLayout(toolLayout)
        layout.addWidget(tableView)

    def addTask(self):
        newTaskItem = TaskItem('new task')
        print newTaskItem.finished
        print newTaskItem.priority
        taskCount = self.model.rowCount()
        print 'setting new item at', str(taskCount)
        self.model.appendRow(newTaskItem)
        

    def connectSignalsWithSlots(self):
        self.addButton.clicked.connect(self.addTask)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication([])
    w = ToDoPanel()
    w.show()
    sys.exit(app.exec_())
_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside

Reply via email to