0

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:

Table output 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:

Updated table image

musicamante
  • 41,230
  • 6
  • 33
  • 58
Huster
  • 3
  • 3
  • 1
    See [Resize column width to fit into the QTableWidget pyqt](https://stackoverflow.com/q/40995778). Add `table.setSizeAdjustPolicy(table.AdjustToContents)` `table.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)` – musicamante Feb 16 '22 at 19:59
  • Thank you so much, it partially solved the problem but still there is a little margin outside the table. – Huster Feb 16 '22 at 21:25
  • @musicamante I will add the updated table image to the question, please check it. – Huster Feb 16 '22 at 21:25
  • You are using [layout managers](//doc.qt.io/qt-5/layout.html) managers, aren't you? – musicamante Feb 16 '22 at 21:53
  • @musicamante Thanks for your help, I solved it. I made a comment to the question with the steps I made to solve it. – Huster Feb 17 '22 at 02:39

1 Answers1

0

I solved the problem and I would like to share the solution with you.
First I added these lines which is mention by @musicamante to my code:


table.setSizeAdjustPolicy(table.AdjustToContents) 
table.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)

They partially solved my problem, but there was a little white space outside the table as the following image:

image after first update

So that, I added this line to completely solve the problem:


table.setStyleSheet("padding :-11px")

final image

musicamante
  • 41,230
  • 6
  • 33
  • 58
Huster
  • 3
  • 3
  • 1
    Sorry, but that's not a good solution: fixed pixel adjustments are always discouraged, especially for size contexts: they don't consider system settings and are not DPI aware. It'll probably work on *your* system, not in others'. You also didn't answer my question about layouts (which is of utmost importance: avoiding layout managers is almost always a terrible practice). Also, please don't use the `\` character in posts, as it's completely ignored. Please read more carefully the [code practice](//meta.stackoverflow.com/a/251362) and read the format help in the post editor (see the [?] link). – musicamante Feb 17 '22 at 03:20