Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

python - Resize column width to fit into the QTableWidget pyqt

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

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.

With a picture it should be all clear:

enter image description here

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

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

You then just need to do:

    self.statTable.resizeColumnsToContents()

whenever the table is re-populated.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...