如何按比例调整 QTableView 中的列宽? [英] How to proportionally adjust column widths in a QTableView?
问题描述
我想按比例更改 QTableView 小部件中所有列的列宽,以便无论数据如何,每列都具有相同的宽度.例如,如果一个表格有三列,每列的宽度应始终为可用水平空间的三分之一 - 并且每当用户调整对话框大小时,宽度应自动更新.
I want to proportionally change the column width of all columns in a QTableView widget, so that each column has the same width regardless of the data. For example, if a table has three columns, each column should always have a width of one third of the available horizontal space - and the width should be automatically updated whenever the dialog is resized by the user.
到目前为止,我只设法将列的大小调整为它们的内容,这不是我想要的.这是我目前得到的代码:
So far I've only managed to resize columns to their contents, which is not what I want. Here's the code I've got so far:
main.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>624</width>
<height>329</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTableView" name="tableView">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPushButton" name="btnPopulate">
<property name="text">
<string>Populate</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>
<tabstop>btnPopulate</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>
test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
from PyQt5 import uic
from PyQt5.QtGui import QStandardItemModel
from PyQt5.QtWidgets import QDialog, QApplication, QHeaderView
class GUI(QDialog):
def __init__(self):
super(GUI, self).__init__()
dirname = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(dirname,'main.ui'), self)
# button
self.btnPopulate.clicked.connect(self.populate)
# table model
self.header = ['col1', 'col2', 'col3']
self.QSModel = QStandardItemModel()
self.QSModel.setColumnCount(3)
self.QSModel.setHorizontalHeaderLabels(self.header)
# table view
self.tableView.setModel(self.QSModel)
self.tableView.setWordWrap(True)
self.tableView.horizontalHeader().setStretchLastSection(False)
def populate(self):
self.longtext = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ac tellus nunc. Phasellus imperdiet leo metus, et gravida lacus. Donec metus ligula, elementum at pellentesque pellentesque, suscipit ac nunc.'''
row = self.QSModel.rowCount()
for x in range(7):
self.QSModel.insertRow(row)
self.QSModel.setData(self.QSModel.index(row, 0), 'Lorem ipsum')
self.QSModel.setData(self.QSModel.index(row, 1), self.longtext)
self.QSModel.setData(self.QSModel.index(row, 2), 'Lorem ipsum')
self.tableView.resizeColumnsToContents()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = GUI()
window.show()
sys.exit(app.exec_())
我有以下问题:
- 如何更改代码以按比例调整列宽?
- 为什么
setWordWrap(True)
不包裹文本?
- How do I change the code to proportionally adjust the column widths?
- Why doesn't
setWordWrap(True)
wrap the text?
推荐答案
这可以通过 设置节大小调整模式.要获得相等的列宽:
This can be achieved by setting the section resize mode. To get equal column widths:
self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
垂直包裹内容:
self.tableView.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
无需调用resizeToContents
、setWordWrap
或setStretchLastSection
.调用 setWordWrap(False)
将切换到省略右侧的文本,而不是换行.
There is no need to call resizeToContents
, setWordWrap
or setStretchLastSection
. Calling setWordWrap(False)
will switch to eliding the text on the right, rather than wrapping.
请注意,由于行/列大小调整是自动完成的,因此用户或以编程方式无法再更改大小.
Note that since the row/column resizing is done automatically, the sizes can no longer be changed by the user or programmatically.
这篇关于如何按比例调整 QTableView 中的列宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!