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

标签 python resize pyqt4 qtableview

我在正确调整表格 View 中的行大小时遇到​​了一个小问题。 我有一个垂直标题,没有水平标题。 我尝试过:

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)

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

如何强制行适合数据?

最佳答案

对我来说,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

关于python - 如何在 PyQt4 中使用 setRowHeight 和 resizeRowToContents 调整行大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40019022/

相关文章:

c - 在运行时将 char[x] 调整为 char[y]

python - 如何将 MacPort 的 pyqt 链接到 Enthought 的 Python 发行版?

python - 如何使用生成器在 Python 中获取笛卡尔积?

python - 使用视锥体检查边界框相交/碰撞

python - VS Code 在 Mac 上保存 Python 文件需要很长时间

python - 为什么dictproxy会改变它的id?

c# - 调整 C# Windows 窗体大小时防止窗口重绘

java jframe窗口不调整大小

python - 如何更改标签的字体(和字体大小)

Python PyQt4 QTableWidget 标题样式表无法正常工作