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. How to set the default size of column in QT Tree view with longest member of Column
Qt 6.11 is out! See what's new in the release blog

How to set the default size of column in QT Tree view with longest member of Column

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 2.0k Views 2 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.
  • A Offline
    A Offline
    Ayush Gupta
    wrote on last edited by
    #1

    I need to set the defaut size of Column in QT tree view the longest name member of Column.

    How can I get the. I mean how can I get the width of Longest name member of Column in QT tree view.
    So I can make the default size of column to that.

    Is there any function for that in QT?

    Pl45m4P 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Since you write the model you've access to your data and can find out the longest string.

      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
      0
      • A Ayush Gupta

        I need to set the defaut size of Column in QT tree view the longest name member of Column.

        How can I get the. I mean how can I get the width of Longest name member of Column in QT tree view.
        So I can make the default size of column to that.

        Is there any function for that in QT?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @Ayush-Gupta

        You could try to work with your QTreeViews Header. (treeView->header())

        https://doc.qt.io/qt-5/qheaderview.html#ResizeMode-enum

        (Call ResizeToContent everytime you add a new branch to your model, so it will always have the max length)
        Edit: You dont need to call it, since it's a Resizing Strategy. It should update the size by itself.

        The actual "length" of a word depends on font (pixel / point sizes) and letters, so I dont think it will work by counting and comparing single characters. (-> "WWW", 3 letters, needs more space than 4 capital i`s)


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        A 1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @Ayush-Gupta

          You could try to work with your QTreeViews Header. (treeView->header())

          https://doc.qt.io/qt-5/qheaderview.html#ResizeMode-enum

          (Call ResizeToContent everytime you add a new branch to your model, so it will always have the max length)
          Edit: You dont need to call it, since it's a Resizing Strategy. It should update the size by itself.

          The actual "length" of a word depends on font (pixel / point sizes) and letters, so I dont think it will work by counting and comparing single characters. (-> "WWW", 3 letters, needs more space than 4 capital i`s)

          A Offline
          A Offline
          Ayush Gupta
          wrote on last edited by
          #4

          @Pl45m4 This is causing performace issue Hence I thought to use set the column to default size

          JonBJ 1 Reply Last reply
          0
          • A Ayush Gupta

            @Pl45m4 This is causing performace issue Hence I thought to use set the column to default size

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

            @Ayush-Gupta
            If you're not prepared to go via ResizeToContents, you yourself have to measure every item in the correct font etc. and use that to set a width.

            1 Reply Last reply
            1
            • A Offline
              A Offline
              Ayush Gupta
              wrote on last edited by
              #6

              when we apply setSectionResizeMode(Qt::HeaderView::ResizetoContents)
              tree view will resize automatically. Is there any way we can disable that after making the call
              setSectionResizeMode in constructor

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

                Hi,

                Set the mode you want to use and the call QHeaderView::resizeSections. That should do what you want.

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

                A 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  Set the mode you want to use and the call QHeaderView::resizeSections. That should do what you want.

                  A Offline
                  A Offline
                  Ayush Gupta
                  wrote on last edited by
                  #8

                  @SGaist can give some code snippet?

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

                    A snippet of what ?

                    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

                    • Login

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