Solved Responsive Text in QHeaderView
-
I'm trying to make the text in a QHeaderView responsive to column width changes, so if it's under a certain pixel width the text becomes truncated. I tried creating a delegate to modify the text in the delegate's paint function, but after reading the documentation it states that delegates in QHeaderViews are ignored:
Note: Each header renders the data for each section itself, and does not rely on a delegate. As a result, calling a header's setItemDelegate() function will have no effect.
I only have two ideas now for how to do this:
- Subclass QHeaderView and implement custom painting
- Pass the QHeaderView directly to the model and return the appropriate text based on column sizes.
The first idea seems excessive for just getting truncated text to be displayed, and the second idea tightly couples the view and model.
Is there a more elegant way to accomplish this?
-
Then connect to the sizeChange signal and set the text you want.
-
Did you already know QHeaderView::elideMode - it automatically elides the text when needed.
-
Sorry, I should have been more specific. It's not exactly truncated; I want to display a substitute string when the column width is under a certain width. So "Quantity" would become "Qty", for instance.
-
Then connect to the sizeChange signal and set the text you want.