[PyQt] setItemDelegateForColumn(...) not setting properly?

Kurt Schwarz kurtschwarz at gmail.com
Sun Feb 6 23:37:48 GMT 2011


I'm not sure if this is a bug or I'm just doing it wrong. If you
use setItemDelegateForColumn on multiple columns only the last assigned
delegate will actually be called. I have only run this code on PyQt 4.8.2.

Example:
import sys
from PyQt4 import QtCore, QtGui

class TestWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(TestWidget, self).__init__(parent)
        self.layout = QtGui.QVBoxLayout()
        self.createTableView()
        self.setLayout(self.layout)
    def createTableView(self):
        model = QtGui.QStandardItemModel(5, 5)
        model.setHeaderData(0, QtCore.Qt.Horizontal, 'Column #0')
        model.setHeaderData(1, QtCore.Qt.Horizontal, 'Column #1')
        model.setHeaderData(2, QtCore.Qt.Horizontal, 'Column #2')
        model.setHeaderData(3, QtCore.Qt.Horizontal, 'Column #3')
        model.setHeaderData(4, QtCore.Qt.Horizontal, 'Column #4')
        table = QtGui.QTableView()
        table.setModel(model)
        table.verticalHeader().setVisible(False)
        delegate_0 = Column0Delegate()
        table.setItemDelegateForColumn(0, delegate_0)
        delegate_1 = Column1Delegate()
        table.setItemDelegateForColumn(1, delegate_1)
        delegate_2 = Column2Delegate()
        table.setItemDelegateForColumn(2, delegate_2)
        delegate_3 = Column3Delegate()
        table.setItemDelegateForColumn(3, delegate_3)
        self.layout.addWidget(table)

class Column0Delegate(QtGui.QItemDelegate):
    def __init__(self):
        super(Column0Delegate, self).__init__()
    def paint(self, painter, options, index):
        print 'Column0Delegate.paint()'

class Column1Delegate(QtGui.QItemDelegate):
    def __init__(self):
        super(Column1Delegate, self).__init__()
    def paint(self, painter, options, index):
        print 'Column1Delegate.paint()'

class Column2Delegate(QtGui.QItemDelegate):
    def __init__(self):
        super(Column2Delegate, self).__init__()
    def paint(self, painter, options, index):
        print 'Column2Delegate.paint()'

class Column3Delegate(QtGui.QItemDelegate):
    def __init__(self):
        super(Column3Delegate, self).__init__()
    def paint(self, painter, options, index):
        print 'Column3Delegate.paint()'

def main(argv):
    app = QtGui.QApplication(argv)
    window = TestWidget()
    window.show()
    return app.exec_()

if __name__ == '__main__':
    sys.exit(main(sys.argv))

According to itemDelegateForColumn the delegate is being assigned...

<__main__.Column0Delegate object at 0x023106F0>
<__main__.Column1Delegate object at 0x02310738>
<__main__.Column2Delegate object at 0x02310780>
<__main__.Column3Delegate object at 0x023107C8>

However when the program runs it only prints "Column3Delegate.paint()" when
it should print all 4 of those messages. It appears that only the last
assigned delegate is actually being called.

Am I doing something wrong here or is this a bug?

Thanks!

-- 
Best Regards,
Kurt Schwarz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110206/74f07f86/attachment.html>


More information about the PyQt mailing list