PyQt Set column widths

百般思念 提交于 2020-02-21 13:20:18

问题


I've got a simple QTableView and I essentially want to emulate this:

view.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)

However, I cannot actually use this, since this calls sizeHint on every single row, which is unacceptable in my case, since the widget needs to be responsive on startup and the calculations take a long time for all the rows. Therefore this is not an option.

I know what the size of a single column, which is always the same, and is always the one with largest height. However, the width of this cell may not always be the widest of all the cells in a row. I've made those cells the correct size by doing this, but this obviously makes all the cells the same size, and many are now far too wide.

view.horizontalHeader().setDefaultSectionSize(200)
view.verticalHeader().setDefaultSectionSize(100)

I've also tried to set individual column widths, but that seems to have no effect at all on the widths, like this:

view.setColumnWidth(0, 5)

Also, setting individual column widths isn't that great, since I can't know in advance how wide they're going to be.

Is there any way to use (or emulate) the ResizeToContents approach (as shown above), but in a way that wouldn't require checking all the cell sizes?


回答1:


Have you tried to optimize the ResizeToContents mode with setResizeContentsPrecision ?

Sets how precise QHeaderView should calculate the size when ResizeToContents is used. A low value will provide a less accurate but fast auto resize while a higher value will provide a more accurate resize that however can be slow.

The number precision specifies how many sections that should be consider when calculating the preferred size.

The default value is 1000 meaning that a horizontal column with auto-resize will look at maximum 1000 rows on calculating when doing an auto resize.

Special value 0 means that it will look at only the visible area. Special value -1 will imply looking at all elements.



来源:https://stackoverflow.com/questions/44978217/pyqt-set-column-widths

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