调整列宽以适应 QTableWidget pyqt [英] Resize column width to fit into the QTableWidget pyqt

查看:144
本文介绍了调整列宽以适应 QTableWidget pyqt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用谷歌搜索过,但找不到解决问题的方法.

I've googled around but I'm not able to find a solution to my problem.

我有一个带有 2 列的 QTableWidget,我想要做的是让它们对整个小部件可见,而不会出现水平滚动条.

I have a QTableWidget with 2 columns and what I'm trying to do is to make them visible to the whole widget without the horizontal scrollbar to appear.

一张照片应该很清楚:

我使用 Qt Designer 创建了用户界面和一些代码来填充所有小部件和其他东西.

I have used Qt Designer to create the UI and some code to fill all the widgets and other stuff.

所以,首先我将 th2 2 列调整为内容:

So, first I resized th2 2 columns to the content with:

self.statTable.resizeColumnToContents(0)
self.statTable.resizeColumnToContents(1)

它可以工作,但是小部件没有调整到 2 列宽度.

and it works, but then the Widget is not resizing to the 2 columns width.

推荐答案

这在 PyQt5 中有一个非常简单的解决方案.您需要做的就是设置尺寸调整策略table 初始化 UI 时,它会自动调整大小以适应内容.这可以通过 Qt 设计器(在属性编辑器的 QAbstractScrollArea 部分)或以编程方式完成,如下所示:

This has a very easy solution in PyQt5. All you need to do is set the size adjust policy on the table when initialising the UI, and it will automatically resize to fit the contents. This can either be done via Qt Designer (in the QAbstractScrollArea section of the Property Editor), or programmatically, like this:

    self.statTable.setSizeAdjustPolicy(
        QtWidgets.QAbstractScrollArea.AdjustToContents)

然后你只需要:

    self.statTable.resizeColumnsToContents()

每当表重新填充时.

对于 PyQt4,一切都必须手动计算,还需要一些 hack 才能获得完全一致的结果.下面的演示脚本对我来说没问题,但是 YMMV:

For PyQt4, everything has to be calculated manually, and a few hacks are also required to get completely consistent results. The demo script below works okay for me, but YMMV:

import random
from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.table = QtGui.QTableWidget(5, 2, self)
        self.button = QtGui.QPushButton('Populate', self)
        self.button.clicked.connect(self.populate)
        layout = QtGui.QGridLayout(self)
        layout.addWidget(self.table, 0, 0)
        layout.addWidget(self.button, 1, 0)
        layout.setColumnStretch(1, 1)

    def populate(self):
        words = 'Red Green Blue Yellow Black White Purple'.split()
        length = random.randint(2, len(words))
        self.table.setRowCount(random.randint(3, 30))
        for column in range(self.table.columnCount()):
            for row in range(self.table.rowCount()):
                item = QtGui.QTableWidgetItem(' '.join(
                    random.sample(words, random.randint(1, length))))
                self.table.setItem(row, column, item)

        self.table.setVisible(False)
        self.table.verticalScrollBar().setValue(0)
        self.table.resizeColumnsToContents()
        self.table.setVisible(True)
        self.setTableWidth()

    def setTableWidth(self):
        width = self.table.verticalHeader().width()
        width += self.table.horizontalHeader().length()
        if self.table.verticalScrollBar().isVisible():
            width += self.table.verticalScrollBar().width()
        width += self.table.frameWidth() * 2
        self.table.setFixedWidth(width)

    def resizeEvent(self, event):
        self.setTableWidth()
        super(Window, self).resizeEvent(event)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(700, 150, 800, 400)
    window.show()
    sys.exit(app.exec_())

这篇关于调整列宽以适应 QTableWidget pyqt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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