Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Resize TableView column based on contents in a column
Forum Updated to NodeBB v4.3 + New Features

Resize TableView column based on contents in a column

Scheduled Pinned Locked Moved QML and Qt Quick
12 Posts 3 Posters 7.6k Views 1 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.
  • R Offline
    R Offline
    ruysch
    wrote on last edited by
    #1

    How can I resize the contents of a TableView column based upon the contents in a column?

    My TableView is declared more or less like this in QML:

    @
    TableView {
    ...
    ...
    TableViewColumn {
    title: "ID#"
    role: "id"
    width: 96
    resizeable: false
    }
    }
    @

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      How about using "resizeColumnsToContents":http://qt-project.org/doc/qt-5/qml-qtquick-controls-tableview.html#resizeColumnsToContents-method ?

      157

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ruysch
        wrote on last edited by
        #3

        I would like to ensure column width is set to a variable minimum regardless of contents

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Sorry but I’m not understanding. Do you mean you want to explicitly set the width ? and it should be based upon the contents ? Can you elaborate ?

          157

          1 Reply Last reply
          0
          • R Offline
            R Offline
            ruysch
            wrote on last edited by
            #5

            I wish to set width programmatically based upon contents but with a minimum size, the resizeColumnsToContents doesnt cut it for me.

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              AFAIK there's no such property to set. But you could easily do it by iterating over the contents of TableView's particular column of interest and then storing the length of the text as per your condition. This can then be used to set the width of that particular column.
              @
              tableview.getColumn(indexofcolumn).width = length-of-text*some-multiplication-factor
              @

              157

              1 Reply Last reply
              0
              • R Offline
                R Offline
                ruysch
                wrote on last edited by
                #7

                How do you get "length-of-text" ?

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  Using JavaScript String length Property.

                  157

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    Jens
                    wrote on last edited by
                    #9

                    ruysch: It is probably a bad idea to do this though. TableView doesn't know the width of items that are not visible on screen. This is because a model could have millions of data entries, and would have to actually fetch and instantiate every single one of them in order to calculate the minimum width. Obviously this will not scale well, so resizeToContents will only give you the with of items currently in view. Perhaps you could rather solve this by creating a tool tip or open an extended view if the data doesn't fit on the current column width.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      ruysch
                      wrote on last edited by
                      #10

                      Jens: To me it appears as if "resizeColumnsToContents" doesnt consider that the column header might actually be larger than the contents, is this true?

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        Jens
                        wrote on last edited by
                        #11

                        ruysch: I believe that is a known issue. I think this would be the appropriate task for it: https://bugreports.qt-project.org/browse/QTBUG-36518

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          ruysch
                          wrote on last edited by
                          #12

                          jens: How would you make certain that resizeColumnsToContents is envoked automagic?

                          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