How do I resize rows with setRowHeight and resizeRowToContents in PyQt4?
<p>I have a small issue with proper resizing of rows in my tableview.
I have a vertical header and no horizontal header.
I tried:</p>

<pre><code>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)
</code></pre>

<p>And this works fine, but when I try:</p>

<pre><code> for n in xrange(self.Popup.table.model().rowCount()):
     self.Popup.table.setRowHeight(n,100)
</code></pre>

<p>or </p>

<pre><code>for n in xrange(self.Popup.table.model().rowCount()):
     self.Popup.table.resizeRowToContents(n)
</code></pre>

<p>No row is resized, even if the text exceeds the length of the cell.</p>

<p>How can I force the rows to fit the data?</p>

<p>For me, both <code>setRowHeight</code> and <code>resizeRowsToContents</code> work as expected. Here's the test script I used:</p>

<pre><code>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_())
</code></pre>

<p>And here's what it looks like:</p>

<p><a href="https://i.stack.imgur.com/mAAA6.png" rel="nofollow"><img src="https://i.stack.imgur.com/mAAA6.png" alt="enter image description here"></a> </p>

python pyqt4 qtableview