如何使用Delegate控制QTableView的行高

How to use Delegate to control QTableView's rows height

此代码创建一个 QTableView,并将 QSpinBoxes 设置为其委派项。我想根据这里描述的技术使用 SpinBoxDelegatesizeHint() 方法来 return 一个固定的 QSize(64,64)Adjusting the height of a QTableView Row

不幸的是,指定的 QSize(64,64) 仅针对单个行 #1 (?) returned。为什么不是每一行都发生?代码哪里出错了?

import sip
sip.setapi('QVariant', 2)

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class SpinBoxDelegate(QItemDelegate):
    def createEditor(self, parent, option, index):
        editor = QSpinBox(parent)
        editor.setMinimum(0)
        editor.setMaximum(100)

        return editor

    def setEditorData(self, spinBox, index):
        value = index.model().data(index, Qt.EditRole)

        spinBox.setValue(value)

    def setModelData(self, spinBox, model, index):
        spinBox.interpretText()
        value = spinBox.value()

        model.setData(index, value, Qt.EditRole)

    def updateEditorGeometry(self, editor, option, index):
        print option
        editor.setGeometry(option.rect)
    def sizeHint(self, option, index):
        print 'sizeHint', index.row(), index.column()
        return QSize(64,64)

if __name__ == '__main__':

    import sys

    app = QApplication(sys.argv)

    model = QStandardItemModel(4, 2)
    tableView = QTableView()
    tableView.setModel(model)

    delegate = SpinBoxDelegate()
    tableView.setItemDelegate(delegate)

    for row in range(4):
        for column in range(2):
            index = model.index(row, column, QModelIndex())
            model.setData(index, (row + 1) * (column + 1))

    tableView.resizeRowToContents(True)

    tableView.show()
    sys.exit(app.exec_())

错误是使用 tableView.resizeRowToContents(True) 而不是 tableView.resizeRowsToContents() 来更新所有当前可见的 QModelIndexes。固定代码如下:

import sip
sip.setapi('QVariant', 2)

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class SpinBoxDelegate(QItemDelegate):
    def createEditor(self, parent, option, index):
        editor = QSpinBox(parent)
        editor.setMinimum(0)
        editor.setMaximum(100)

        return editor

    def setEditorData(self, spinBox, index):
        value = index.model().data(index, Qt.EditRole)

        spinBox.setValue(value)

    def setModelData(self, spinBox, model, index):
        spinBox.interpretText()
        value = spinBox.value()

        model.setData(index, value, Qt.EditRole)

    def updateEditorGeometry(self, editor, option, index):
        print option
        editor.setGeometry(option.rect)
    def sizeHint(self, option, index):
        print 'sizeHint', index.row(), index.column()
        return QSize(64,64)

if __name__ == '__main__':

    import sys

    app = QApplication(sys.argv)

    model = QStandardItemModel(4, 2)
    tableView = QTableView()
    tableView.setModel(model)

    delegate = SpinBoxDelegate()
    tableView.setItemDelegate(delegate)

    for row in range(4):
        for column in range(2):
            index = model.index(row, column, QModelIndex())
            model.setData(index, (row + 1) * (column + 1))

    tableView.resizeRowsToContents()

    tableView.show()
    sys.exit(app.exec_())

或者这里是相同概念的骨架版本:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ForSizeOnlyDelegate(QItemDelegate):
    def sizeHint(self, option, index):
        print 'sizeHint', index.row(), index.column()
        return QSize(64,64)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    model = QStandardItemModel(14, 12)
    tableView = QTableView()
    tableView.setModel(model)

    delegate = ForSizeOnlyDelegate()

    tableView.setItemDelegate(delegate)
    tableView.resizeRowsToContents()

    tableView.show()
    sys.exit(app.exec_())