HTML in QTableWidget
Moved
Solved
Qt for Python
-
Hello,
I have a minimal HTML Rich Text from a database, which I need to display in a simple table.I am using a QTableWidget, I create a QWidget which I add label to, which has my Rich Text.
The markdown works perfectly, <b> are displayed bold et cetera, line breaks with <br> also work but overflow.def newRow(self, fieldName, values): self.tableWidget.setRowCount(self.rowIndex + 1) item = QtWidgets.QTableWidgetItem() item.setText(fieldName) self.tableWidget.setVerticalHeaderItem(self.rowIndex, item) index = 0 for value in values: widget = QtWidgets.QWidget() widgetText = QtWidgets.QLabel(value) widgetText.setWordWrap(True) widgetLayout = QtWidgets.QHBoxLayout() widgetLayout.addWidget(widgetText) widgetLayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize) widget.setLayout(widgetLayout) self.tableWidget.setCellWidget(self.rowIndex,index, widget) index += 1 self.tableWidget.resizeColumnsToContents() self.rowIndex += 1 [...] ui.newRow("Hello Qt", ["no", "no", "no", "<b>Test</b><br>test<br>test"])
And this results in:
How can I adjust the height of the table elements to correctly represent the content, especially the line breaks?
-
The solution is https://doc.qt.io/qt-6/qtableview.html#resizeRowsToContents