I'm trying to achieve a pyqt5 table similar to the following one:
First of all: I am not even sure that QTableWidget is the most appropiate widget, as I do not have much experience with Qt. QGridLayout could fit better. I tried it first but somehow I didn't know how to make it work to fit the requirements, any advice would be very much appreciated.
The table requirements are:
- Some cells contain text, some QCheckBoxes and some QRadioButtons
- The table width must always fill 100% of its parent's width as it resizes (I'm currently using
setSectionResizeMode(QHeaderView.Stretch)) - The table should be vertically scrollable
- Some of its columns have a different relative width, and this relation must be kept (here start my problems)
- The centering of the columns is not the same for every one (couldn't find how to achieve this in the docs)
My current problem is that the use of setSectionResizeMode conflicts with resizeSection. I can have different column widths OR stretchable table width, but I don't know how to achieve both.
The example code looks like this:
from PyQt5.QtWidgets import QApplication, QWidget, QHeaderView, QTableWidget, QTableWidgetItem, QVBoxLayout
import sys
data = {'col1':['1','2','3','4'],
'col2':['1','2','1','3'],
'col3':['1','1','2','1']}
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.table = QTableWidget()
self.table.setRowCount(4)
self.table.setColumnCount(3)
self.table.verticalHeader().hide()
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# self.table.horizontalHeader().resizeSection(1, 200) <-- no effect if stretch is activated
self.table.setHorizontalHeaderLabels([colName for colName in data])
self.fillTable()
self.layout = QVBoxLayout()
self.layout.addWidget(self.table)
self.setLayout(self.layout)
def fillTable(self):
for colNumber, colName in enumerate(data):
for rowNumber, value in enumerate(data[colName]):
self.table.setItem(rowNumber, colNumber, QTableWidgetItem(value))
if __name__=="__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
Here's my other attempt, using a QGridLayout. The problem here is that the columns do not expand when the window is resized.
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout, QLabel, QPushButton, QApplication, QScrollArea, QVBoxLayout)
from PyQt5.QtCore import Qt
class MainWindow(QWidget):
def __init__(self):
super().__init__()
# Label
self.label = QLabel('Test')
# Grid
self.grid = QWidget()
self.gridLayout = QGridLayout()
self.grid.setLayout(self.gridLayout)
self.grid.setMinimumWidth(600) # Without this, the width relations are not kept
# Grid elements
for i in range(20):
self.gridLayout.addWidget(QPushButton('A'), i, 0, 1, 1)
self.gridLayout.addWidget(QPushButton('B'), i, 1, 1, 3)
self.gridLayout.addWidget(QPushButton('C'), i, 4, 1, 1)
# Scroll area
self.scrollArea = QScrollArea()
self.scrollArea.setWidget(self.grid)
self.scrollArea.setMinimumWidth(self.grid.sizeHint().width())
self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# Window layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.label)
self.layout.addWidget(self.scrollArea)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
windowExample = MainWindow()
windowExample.show()
sys.exit(app.exec_())

setSectionResizeModefor individual columns, instead of setting it for all the columns? - musicamante