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. Responsive Text in QHeaderView
Qt 6.11 is out! See what's new in the release blog

Responsive Text in QHeaderView

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.3k 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.
  • Q Offline
    Q Offline
    Qt_User72653
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Then connect to the sizeChange signal and set the text you want.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Did you already know QHeaderView::elideMode - it automatically elides the text when needed.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        Q 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          Did you already know QHeaderView::elideMode - it automatically elides the text when needed.

          Q Offline
          Q Offline
          Qt_User72653
          wrote on last edited by
          #3

          @Christian-Ehrlicher

          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.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Then connect to the sizeChange signal and set the text you want.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3

            • Login

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