PySide - PyQt:如何将 QTableWidget 列宽设置为可用空间的比例? [英] PySide - PyQt : How to make set QTableWidget column width as proportion of the available space?

查看:53
本文介绍了PySide - PyQt:如何将 QTableWidget 列宽设置为可用空间的比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PySide 开发计算机应用程序,并且我正在使用 QTableWidget.假设我的表有 3 列,但它们包含的数据非常不同,例如(对于每一行)第一列中有一个长句子,然后是最后两列中的 3 位数字.我希望调整我的表格大小以根据数据调整其大小,或者至少能够将列大小设置为(例如)70/15/15 %可用空间.

最好的方法是什么?

我在阅读

以下是 ResizeToContents

解决方案

这可以通过为每一列设置 resize-mode 来解决.第一部分必须伸展以占据可用空间,而最后两部分只是调整其内容大小:

PyQt4:

header = self.table.horizo​​ntalHeader()header.setResizeMode(0, QtGui.QHeaderView.Stretch)header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)

PyQt5:

header = self.table.horizo​​ntalHeader()header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)

I'm developing a computer application with PySide and I'm using the QTableWidget. Let's say my table has 3 columns, but the data they contain is very different, like (for each row) a long sentence in the first column, then 3-digit numbers in the two last columns. I'd like to have my table resize in order to adjust its size to the data, or at least to be able to set the column sizes as (say) 70/15/15 % of the available space.

What is the best way to do this ?

I've tried table.horizontalHeader().setResizeMode(QHeaderView.Stretch) after reading this question but it makes 3 columns of the same size.

I've also tried table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) thanks to Fabio's comment but it doesn't fill all the available space as needed.

Neither Interactive, Fixed, Stretch, ResizeToContents from the QHeaderView documentation seem to give me what I need (see second edit).

Any help would be appreciated, even if it is for Qt/C++ ! Thank you very much.


EDIT : I found kind of a workaround but it's still not what I'm looking for :

header = table.horizontalHeader()
header.setResizeMode(QHeaderView.ResizeToContents)
header.setStretchLastSection(True)

It would be better if there existed a setStretchFirstSection method, but unfortunately there does not seem to be one.


EDIT 2 :

The only thing that can be modified in the table is the last column, the user can enter a number in it. Red arrows indicates what I'd like to have.

Here's what happens with Stretch

Here's what happens with ResizeToContents

解决方案

This can be solved by setting the resize-mode for each column. The first section must stretch to take up the available space, whilst the last two sections just resize to their contents:

PyQt4:

header = self.table.horizontalHeader()
header.setResizeMode(0, QtGui.QHeaderView.Stretch)
header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)

PyQt5:

header = self.table.horizontalHeader()       
header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)

这篇关于PySide - PyQt:如何将 QTableWidget 列宽设置为可用空间的比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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