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. QTreeView resize section to content but keep interactive

QTreeView resize section to content but keep interactive

Scheduled Pinned Locked Moved Solved General and Desktop
qtreeviewqheaderview
3 Posts 2 Posters 1.9k 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.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 31 Oct 2022, 13:58 last edited by Joel Bodenmann
    #1

    I'm trying to figure out how to setup/configure a QTreeView so that I one or more section can be programmatically resized to fit the content while still giving the user the ability to resize the section interactively afterwards.

    From the corresponding QHeaderView documentation:

    QHeaderView::ResizeToContents
    QHeaderView will automatically resize the section to its optimal size based on the contents of the entire column or row. The size cannot be changed by the user or programmatically.

    QHeaderView::Interactive
    The user can resize the section. The section can also be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize. (See also cascadingSectionResizes.)

    I'd basically like a mix of the two: First, resize to fit content programmatically and then allow the user to resize interactively. But this doesn't seem to be an option.
    One "workaround" might be to use setDefaultSectionSize() as mentioned by the documentation of QHeaderView::Interactive. However, I couldn't find any clear indications of what the integer size property would represent. Surely it wouldn't be number of pixels. Most likely some DPI independent unit?
    In any case, this would force me to manually calculate the width needed to fit contents into the corresponding section. In case of text this would require calculating the text size using QFontMetric.

    Is there any better way of achieving what I am after? Am I missing something obvious or is this just a non-standard use-case?

    Edit: Seems like QHeaderView::setDefaultSectionSize() wouldn't work either because I'd want to assign different sizes for each section.

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    G 1 Reply Last reply 31 Oct 2022, 14:44
    0
    • J Joel Bodenmann
      31 Oct 2022, 13:58

      I'm trying to figure out how to setup/configure a QTreeView so that I one or more section can be programmatically resized to fit the content while still giving the user the ability to resize the section interactively afterwards.

      From the corresponding QHeaderView documentation:

      QHeaderView::ResizeToContents
      QHeaderView will automatically resize the section to its optimal size based on the contents of the entire column or row. The size cannot be changed by the user or programmatically.

      QHeaderView::Interactive
      The user can resize the section. The section can also be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize. (See also cascadingSectionResizes.)

      I'd basically like a mix of the two: First, resize to fit content programmatically and then allow the user to resize interactively. But this doesn't seem to be an option.
      One "workaround" might be to use setDefaultSectionSize() as mentioned by the documentation of QHeaderView::Interactive. However, I couldn't find any clear indications of what the integer size property would represent. Surely it wouldn't be number of pixels. Most likely some DPI independent unit?
      In any case, this would force me to manually calculate the width needed to fit contents into the corresponding section. In case of text this would require calculating the text size using QFontMetric.

      Is there any better way of achieving what I am after? Am I missing something obvious or is this just a non-standard use-case?

      Edit: Seems like QHeaderView::setDefaultSectionSize() wouldn't work either because I'd want to assign different sizes for each section.

      G Offline
      G Offline
      Gojir4
      wrote on 31 Oct 2022, 14:44 last edited by
      #2

      Hi @Joel-Bodenmann,

      I think you can probably call void QHeaderView::resizeSections(QHeaderView::ResizeMode mode) "manually", for example when the view is shown, and set the resize mode to interactive only.

      J 1 Reply Last reply 14 Nov 2022, 15:39
      2
      • G Gojir4
        31 Oct 2022, 14:44

        Hi @Joel-Bodenmann,

        I think you can probably call void QHeaderView::resizeSections(QHeaderView::ResizeMode mode) "manually", for example when the view is shown, and set the resize mode to interactive only.

        J Offline
        J Offline
        Joel Bodenmann
        wrote on 14 Nov 2022, 15:39 last edited by
        #3

        @Gojir4 That worked well. I'm not sure how I could have missed this simple solution... Thank you a lot!

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply
        1

        • Login

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