CatamountJack
CatamountJack

Reputation: 160

QTableView object has no attribute 'setItemDelegateforColumn'

I'm trying to set a checkbox in the first column of each row of a QTableView using a custom delegate. Whenever the program runs, I get the following error:

AttributeError: 'PySide.QtGui.QTableView' object has no attribute 'setItemDelegateforColumn'

I'm fairly new to Python, Qt, and model-view programming, but I have a nearly identical sequence of functions in a different module that works flawlessly. I'm familiar with "object has no attribute errors", but I'm as to how an attribute could be missing from a standard (non-custom) object.

Just for kicks I've tried replacing setItemDelegateforColumn() with setItemDelegateforRow() and get the same error. Interestingly though, I've also tried replacing it with just setItemDelegate() and get a whole table full of checkboxes - so that works.

I've use Qt Designer to create an application that has a number of tabs (QTabWidget). Within one of those tabs, I have a table (QTableView). All of the relevant UI information that Qt Designer created is in a file called lac_gui.py. My main program calls a function that runs a query using SQLAlchemy, which returns a list of lists that represent rows in the table as self.notes_data. Once the query is run, the following code is called:

def create_notes_table(self):

    #self.tableView_noteslist = QtGui.QTableView(self)  <-- also tried setting directly as a QTableView object to make sure it wasn't being overridden something else.  Same error.
    self.model_notes = NotesTableModel(self, self.notes_data, self.notes_header)
    self.checkbox_delegate = CheckBoxDelegate(self)
    self.tableView_noteslist.setModel(self.model_notes)
    self.tableView_noteslist.setItemDelegateforColumn(0, self.checkbox_delegate)
    return self.tableView_noteslist

The model that I'm using for this table is in the following class:

class NotesTableModel(QtCore.QAbstractTableModel):

    def __init__(self, parent, notes_data, notes_header, *args):
        QtCore.QAbstractTableModel.__init__(self, parent, *args)
        self.notes_data = notes_data
        self.notes_header = notes_header

    def rowCount(self, parent):
        return len(self.notes_data)

    def columnCount(self, parent):
        if len(self.notes_data) > 0:
            return len(self.notes_data[0])
        else: 
            return 0

    def data(self, index, role):
        if not index.isValid():
            return None
        elif role == QtCore.Qt.TextAlignmentRole:
            return QtCore.Qt.AlignLeft
        elif role != QtCore.Qt.DisplayRole:
            return None
        else:
            return self.notes_data[index.row()][index.column()]

    def headerData(self, col, orientation, role):
        if role == QtCore.Qt.TextAlignmentRole:
            return QtCore.Qt.AlignLeft
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return self.notes_header[col]
        return None

    def sort(self, col, order):
        self.emit(QtCore.SIGNAL("layoutAboutToBeChanged()"))
        self.notes_data = sorted(self.notes_data,
            key=operator.itemgetter(col))
        if order == QtCore.Qt.DescendingOrder:
            self.notes_data.reverse()
        self.emit(QtCore.SIGNAL("layoutChanged()"))

    def flags(self, index):
        if index.column() == 0:
            return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
        else:
            return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

    def setData(self, index, new_value, role):
        if not index.isValid():
            return None
        elif role != QtCore.Qt.EditRole:
            return None

        return self.notes_data[index.row()][index.column()]

Relevant pieces from lac_gui.py (from Qt Designer):

self.tab_notes = QtGui.QWidget()
self.tab_notes.setObjectName("tab_notes")
self.tableView_noteslist = QtGui.QTableView(self.tab_notes)

I can't see where QTableView has been subclassed as something else (and even then, if I understand correctly, it would retain the original methods/attributes, right)?

The code in the other module that works is virtually identical except for the names/references.

(FWIW - Python 3.4, Qt 4.8.5, Pyside 1.2.2)

Thanks in advance!

Upvotes: 0

Views: 3975

Answers (1)

RobbieE
RobbieE

Reputation: 4350

The C++ documentation has the member function with a capital 'F' in the '..For..' part of setItemDelegateForColumn.

I'm fairly certain that must be your problem in the python version.

Upvotes: 1

Related Questions