如何调整PyQt4中setRowHeight和resizeRowToContents行的大小?

2024-10-01 11:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小问题,在我的表视图行大小的适当调整。 我有垂直标题,没有水平标题。 我试过了:

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)

这个很好,但是当我尝试的时候:

^{pr2}$

或者

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

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

如何强制行适应数据?在


Tags: inself视图标题formodeltable水平
1条回答
网友
1楼 · 发布于 2024-10-01 11:19:59

对我来说,setRowHeight和{}都按预期工作。以下是我使用的测试脚本:

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

相关问题 更多 >