I'm trying to make a table using pyqt5
This is my code:
table = QTableWidget()
table.setRowCount(len(dataElementsList)+1)
table.setColumnCount(3)
#Add Table heads items
table.setItem(0, 0, QTableWidgetItem("Name "))
table.setItem(0, 1, QTableWidgetItem("DataType "))
table.setItem(0, 2, QTableWidgetItem("Reference Type "))
#Adding Items to the table
for i in range( len(dataElementsList) ):
name = dataElementsList[i]["dataElementName"] + " "
dataType = dataElementsList[i]["dataElementReference"] + " "
referenceType = dataElementsList[i]["dataElementReferenceType"] + " "
table.setItem(i+1, 0, QTableWidgetItem(name))
table.setItem(i+1, 1, QTableWidgetItem(dataType))
table.setItem(i+1, 2, QTableWidgetItem(referenceType))
#Make Cell stretch with the data in it
header = table.horizontalHeader()
header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
then after displaying the table it will be as this image:
My question is: how to remove all this white space and make it shrink to fit only the table?
Update: After adding these lines of code:
table.setSizeAdjustPolicy(table.AdjustToContents)
table.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
The problem is partially solved, but still there is a margin outside the table. This is the updated image of the table: