Resize column width to fit into the QTableWidget pyqt
Asked Answered
L

5

29

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.

Limitary answered 6/12, 2016 at 12:47 Comment(2)
Did you ever try the solution(s) I gave in my answer? I'd be interested to know if they solved your problem (it was quite tricky to work out, and I'm not sure I covered all the corner cases).Shanklin
Hi, sorry for not responding anymore.. By he way, I solved by adding this line after the two lines of above: self.statTable.setFixedWidth(self.statTable.columnWidth(0) + self.statTable.columnWidth(1))Limitary
S
54

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_())
Shanklin answered 6/12, 2016 at 19:54 Comment(5)
I'm using pyQt5 so where can I find that on QT designer ? cause I don't see it anywhere and typing the code didn't work! @ShanklinGemmiparous
@MohammedAminAIMEUR. It's in the Property Editor (QAbstractScrollArea section).Shanklin
You should pip install pyqt5-tools Qt designer is inside of this package. @GemmiparousArabel
my only issue is that it doesn't make it fit vertically.Cassady
>statTable.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents) - is there a way to adjust only horizontal scrollbar (not both)?Evania
S
8

For the autoadjust settings on the table widget in Qt Designer, you canlook in the object inspector for the table widget you can drill down to it as shown below.

enter image description here

Stirrup answered 27/12, 2018 at 21:58 Comment(0)
A
3

(PyQt5)

The issue for me is that my cells in the right-most column are multi-line (QPlainTextEdit), and I wanted word-wrapping... but I also wanted this right-most column to extend to fill the parent container.

It seems you can do everything you need in PyQt5 designer, in the Property editor for your QTableView:

  • in the "QTableView" section check "wordWrap"
  • in the "QAbstractScroll" section check "AdjustToContents" (as mentioned by Crap Phone)
  • in the "Header" section check "horizontalHeaderStretchLastSection"

This then generates the following sort of code:

self.history_table_view.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContentsOnFirstShow)
self.history_table_view.horizontalHeader().setStretchLastSection(True )

"word wrap = True" appears to be the default setting, so nothing is shown, but it would be this:

self.history_table_view.setWordWrap(True)
Amasa answered 7/11, 2020 at 17:52 Comment(1)
Probably the most elegant way to do it among everything I have read so far. Thanks you.Rufus
H
3

Use your_tablewidget.resizeColumnsToContents() every single time after you call your_tablewidget.setItem(). You don't need any other setting.

Herwig answered 25/9, 2021 at 13:7 Comment(0)
B
-1

you know just try, tableWidget.resize(1200, 600) *you can change resolution but it is your answer...

Borak answered 28/7, 2022 at 14:46 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewShoran

© 2022 - 2024 — McMap. All rights reserved.