如何在PyQt4中使用setRowHeight和resizeRowToContents调整行大小?

时间:2016-10-13 10:54:44

标签: python pyqt4 qtableview

我在tableview中正确调整行的大小问题。 我有一个垂直标题,没有水平标题。 我试过了:

self.Popup.table.setModel(notesTableModel(datainput))
self.Popup.table.horizontalHeader().setVisible(False)
self.Popup.table.verticalHeader().setFixedWidth(200)
for n in xrange(self.Popup.table.model().columnCount()):
    self.Popup.table.setColumnWidth(n,150)

这很好用,但是当我尝试时:

 for n in xrange(self.Popup.table.model().rowCount()):
     self.Popup.table.setRowHeight(n,100)

for n in xrange(self.Popup.table.model().rowCount()):
     self.Popup.table.resizeRowToContents(n)

即使文本超出单元格的长度,也不会调整行的大小。

如何强制行符合数据?

1 个答案:

答案 0 :(得分:3)

对我而言,setRowHeightresizeRowsToContents都按预期工作。这是我使用的测试脚本:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self, rows, columns):
        super(Window, self).__init__()
        self.table = QtGui.QTableView(self)
        self.table.horizontalHeader().setVisible(False)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.table)
        model =  QtGui.QStandardItemModel(rows, columns, self.table)
        self.table.setModel(model)
        text = 'some long item of text that requires word-wrapping'
        for column in range(model.columnCount()):
            self.table.setColumnWidth(column, 150)
            for row in range(model.rowCount()):
                item = QtGui.QStandardItem(text)
                model.setItem(row, column, item)
                # self.table.setRowHeight(row, 100)
        self.table.resizeRowsToContents()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window(4, 3)
    window.setGeometry(800, 150, 500, 250)
    window.show()
    sys.exit(app.exec_())

这就是它的样子:

enter image description here