Struggling with setting column sizes
-
wrote on 23 Jan 2024, 15:22 last edited by Parthe
Hi,
I'm very new to Qt (like, this is my second day of coding with it) so I apologise if I'm missing something basic here, or doing something wrong.
Using Qt6 I have a window I created from the QMainWindow class.
In it I have a VBoxLayout in which I am adding a table created from QTableView.class myMainWindow(QMainWindow): def __init__(self): # Call the super __init__() function to set up the window via Qt. super().__init__() self.setWindowTitle('This is a test window') self.layout = QVBoxLayout() self.mainTable = QTableView() self.layout.addWidget(self.mainTable) self.widget = QWidget() self.widget.setLayout(self.layout) self.setCentralWidget(self.widget)
I then create a table based on TableModel, passing in a list of lists. This creates the table data correctly.
table_data=[ [ 'Mr', 'Joe', 'Baker', 'Mr Baker was a well-known cooked-dough provide in town.', '1944-12-15'] [ 'Mrs', 'Josephine', 'Bloggs', 'Mrs Bloggs (Jo to her friends) was a regular church goer and was held in high estimation in the village', '1949-06-24'] [ 'Mstr, 'Harold', 'Bloggs', 'Young Master Bloggs was a tearabout in his youth, but grew up to join in his fathers business', '1965-05-14'] ] # Store the data in the table self.model = TableModel(table_data)
I then set the model of the QTableView (mainTable) to the TableModel and display the window.
self.mainTable.setModel(self.model) window.show() # IMPORTANT!!!!! Windows are hidden by default. # Start the event loop. app.exec()
The problem is that I cannot work out how to set the column widths automatically. Ideally I want all columns to fit to the width of the data apart from column 4 (the description) which can be overlapped if the main window is too narrow.
From the documentation I've read I should be able to do this using the QHeaderView.Stretch and QHeaderView. ResizeToContents attributes, but I cannot find out where or how I should set these.
Can you help please?
-
Hi,
I'm very new to Qt (like, this is my second day of coding with it) so I apologise if I'm missing something basic here, or doing something wrong.
Using Qt6 I have a window I created from the QMainWindow class.
In it I have a VBoxLayout in which I am adding a table created from QTableView.class myMainWindow(QMainWindow): def __init__(self): # Call the super __init__() function to set up the window via Qt. super().__init__() self.setWindowTitle('This is a test window') self.layout = QVBoxLayout() self.mainTable = QTableView() self.layout.addWidget(self.mainTable) self.widget = QWidget() self.widget.setLayout(self.layout) self.setCentralWidget(self.widget)
I then create a table based on TableModel, passing in a list of lists. This creates the table data correctly.
table_data=[ [ 'Mr', 'Joe', 'Baker', 'Mr Baker was a well-known cooked-dough provide in town.', '1944-12-15'] [ 'Mrs', 'Josephine', 'Bloggs', 'Mrs Bloggs (Jo to her friends) was a regular church goer and was held in high estimation in the village', '1949-06-24'] [ 'Mstr, 'Harold', 'Bloggs', 'Young Master Bloggs was a tearabout in his youth, but grew up to join in his fathers business', '1965-05-14'] ] # Store the data in the table self.model = TableModel(table_data)
I then set the model of the QTableView (mainTable) to the TableModel and display the window.
self.mainTable.setModel(self.model) window.show() # IMPORTANT!!!!! Windows are hidden by default. # Start the event loop. app.exec()
The problem is that I cannot work out how to set the column widths automatically. Ideally I want all columns to fit to the width of the data apart from column 4 (the description) which can be overlapped if the main window is too narrow.
From the documentation I've read I should be able to do this using the QHeaderView.Stretch and QHeaderView. ResizeToContents attributes, but I cannot find out where or how I should set these.
Can you help please?
wrote on 23 Jan 2024, 18:48 last edited byYou can call .QTableView.resizeColumnToContents(column), QTableView.resizeColumnsToContents() ( see https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QTableView.html#PySide6.QtWidgets.PySide6.QtWidgets.QTableView.resizeColumnToContents ) .
-
You can call .QTableView.resizeColumnToContents(column), QTableView.resizeColumnsToContents() ( see https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QTableView.html#PySide6.QtWidgets.PySide6.QtWidgets.QTableView.resizeColumnToContents ) .
wrote on 24 Jan 2024, 12:02 last edited by PartheExcellent! Thanks @friedemannkleint , that did the job nicely.
For the sake of completeness and to help others who might have this issue in the future, I modified my code as follows.
self.mainTable.setModel(self.model) # Now that the table has the data and the view is set to the table, # resize the columns correctly. Ignore column 3 since the contents # can be very large, we want that to overlap. for r in range(self.model.rowCount()): if r == 2: continue self.mainTable.resizeColumnToContents(r)
Next I need to work on setting the resize settings to the columns to negate the need for this, but that's a different problem.
-
1/3