如何在 PyQt4 中使用 setRowHeight 和 resizeRowToContents 调整行大小? [英] How do I resize rows with setRowHeight and resizeRowToContents in PyQt4?

查看:46
本文介绍了如何在 PyQt4 中使用 setRowHeight 和 resizeRowToContents 调整行大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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.

如何强制行适合数据?

推荐答案

对我来说,setRowHeightresizeRowsToContents 都按预期工作.这是我使用的测试脚本:

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

这是它的样子:

这篇关于如何在 PyQt4 中使用 setRowHeight 和 resizeRowToContents 调整行大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆