Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget cells will word wrap, but column headers don't?
Forum Updated to NodeBB v4.3 + New Features

QTableWidget cells will word wrap, but column headers don't?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 4.4k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    donquibeats
    wrote on last edited by
    #1

    I've got a simple QTableWidget in my PyQt5 app, and I am applying setWordWrap(True) on it at the top of my code.

    The cell text is wrapping very nicely, but the column headers are not wrapping at all, and seem restricted to one line.

    Is this expected behaviour? Does the word wrap setting only apply to cell contents, and not to the headers? Ideally I would like the headers to wrap depending on width, just like the cells do.

    I'm setting the column header text with tableWidget.setHorizontalHeaderLabels(columnLabels) where columnLabels is a list of strings ['Name', 'Index', 'Colour'] etc. I can provide a reproducable example if it would help, but perhaps this is just expected behaviour and it wouldn't help?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      See this stack overflow thread.

      There are several suggestions there that looks like what you want. Read them all as there are several possibilities that you want to try.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • D Offline
        D Offline
        donquibeats
        wrote on last edited by
        #3

        Thanks once again. My Stack Overflow searches had somehow missed that one, probably because I was searching for QTableWidget rather than QTableView.

        Thankfully, I didn't have to get into the top answer and its sub-classing, and there was a simple one-line solution:

        tableWidget.horizontalHeader().setDefaultAlignment(QtCore.Qt.AlignHCenter | Qt.Alignment(QtCore.Qt.TextWordWrap))

        This works exactly in the way I was looking for.

        JonBJ 1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Great !

          Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :-)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D donquibeats

            Thanks once again. My Stack Overflow searches had somehow missed that one, probably because I was searching for QTableWidget rather than QTableView.

            Thankfully, I didn't have to get into the top answer and its sub-classing, and there was a simple one-line solution:

            tableWidget.horizontalHeader().setDefaultAlignment(QtCore.Qt.AlignHCenter | Qt.Alignment(QtCore.Qt.TextWordWrap))

            This works exactly in the way I was looking for.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @donquibeats
            For anyone reading. I found this principle works, in that the words do wrap. However, that made the subsequent lines not visible, I had to do a setMinimumHeight() to give it room.

            1 Reply Last reply
            2

            • Login

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