Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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?


  • Lifetime Qt Champion

    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.



  • 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.


  • Lifetime Qt Champion

    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 :-)



  • @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.


Log in to reply