Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. HTML in QTableWidget
Forum Update on Tuesday, May 27th 2025

HTML in QTableWidget

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 1 Posters 854 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AltayAkkus
    wrote on 28 Sept 2022, 09:54 last edited by
    #1

    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:
    7f482bac-f6b7-4009-91eb-8aa38db11581-image.png

    How can I adjust the height of the table elements to correctly represent the content, especially the line breaks?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AltayAkkus
      wrote on 17 Oct 2022, 09:43 last edited by
      #2

      The solution is https://doc.qt.io/qt-6/qtableview.html#resizeRowsToContents

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved