PyQt:How do i set different header sizes for individual headers?
Asked Answered
I

2

10

I have a list containing lists with two items,a word and a number.This list will be presented using a tablewidget.

My aim is to produce a table with two columns and with the neccessary rows,but the header of the column which will have the words should be larger than the numbers column.

I could use resize columns to content,but I want the table without the white empty white space around the table,after the resize.

For the creation of the gui code I am using QtDesigner.Thanks.

Iliac answered 20/12, 2011 at 1:33 Comment(0)
A
20

There are a few methods of the QHeaderView class that will probably do what you want. The simplest is:

table.horizontalHeader().setStretchLastSection(True)

This will ensure that the last column is automatically resized to fit the available space in the table, leaving the width of the other columns as they are (and resizable by the user).

Alternatively, there are methods for setting the ResizeMode of the columns.

For Qt5:

table.setColumnWidth(1, 80)
table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)

For Qt4:

table.setColumnWidth(1, 80)
table.horizontalHeader().setResizeMode(0, QHeaderView.Stretch)

This will fix the width of the second column, and ensure the first column is automatically resized to fill the remaining space (but preventing any other resizing by the user).

Auburn answered 20/12, 2011 at 18:53 Comment(2)
Is there a way to achieve that using Qt Desginer ? Is there an option ?Iliac
There's no option in designer for the ResizeMode, but there is for setStretchLastSection (i.e. Header/horizontalHeaderStrechLastSection in the Property Editor).Auburn
H
8

the best solution for this, in Qt5 you have to use setSectionResizeMode instead of setResizeMode:

tabv = QTableView()
tabv.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

or

tabv.horizontalHeader().setSectionResizeMode(1)
Hartal answered 29/7, 2015 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.