【问题标题】:How do I resize rows with setRowHeight and resizeRowToContents in PyQt4?如何在 PyQt4 中使用 setRowHeight 和 resizeRowToContents 调整行的大小?
【发布时间】:2016-10-13 10:54:43
【问题描述】:

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

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)

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

如何强制行适合数据?

【问题讨论】:

  • 不清楚您希望行/列调整大小的方向。你说的是换行吗?
  • 是的,完全正确;我希望行根据该行中单元格的内容增加高度。

标签: python resize pyqt4 qtableview


【解决方案1】:

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

这就是它的样子:

【讨论】:

  • 自动换行对我有用,但我无法手动设置高度;
  • @Mee。你什么意思?您是否尝试在我的脚本中取消注释 setRowHeight 行? (您还需要注释掉 resizeRowsToContents 行)。
猜你喜欢
  • 2012-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-01-26
  • 2017-05-20
  • 2018-11-24
  • 2018-10-06
  • 2017-04-26
  • 1970-01-01
相关资源
最近更新 更多