How do I resize rows with setRowHeight and resizeRowToContents in PyQt4?

扶醉桌前 提交于 2020-05-16 02:28:48

问题


I have a small issue with proper resizing of rows in my tableview. I have a vertical header and no horizontal header. I tried:

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)

And this works fine, but when I try:

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

or

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

No row is resized, even if the text exceeds the length of the cell.

How can I force the rows to fit the data?


回答1:


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

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_())

And here's what it looks like:



来源:https://stackoverflow.com/questions/40019022/how-do-i-resize-rows-with-setrowheight-and-resizerowtocontents-in-pyqt4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!