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. QTableView Stretch Headers but keep Interactive
Forum Updated to NodeBB v4.3 + New Features

QTableView Stretch Headers but keep Interactive

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 4.3k 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
    Adan895
    wrote on last edited by
    #1

    I am struggling triying to accomodate my QTableView to make it easy to the user.

    This functions works as i really need:

    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    The issue with this is that the headers are not user adjustable anymore, are completely frozzen.

    I know we also have this function which allows the user to adjust the Headers, but after using it, the headers go back as if i had not used QHeaderView::Stretch before:

    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
    I have really looked for this on many websites with no luck of finding a understandable answer for my level.

    Thanks!

    JonBJ 1 Reply Last reply
    0
    • A Offline
      A Offline
      Adan895
      wrote on last edited by Adan895
      #11

      Hi Guys, after Struggling like 3 hours ive got it:

      for(int c = 0;c<=4;c++){
              ui->tableView->horizontalHeader()->resizeSection(c, 150);
              }
      
              ui->tableView->horizontalHeader()->setStretchLastSection(true);
      

      Set first the width of each column to match the QTableView size and then Stretch the last section, thanks a lot!

      1 Reply Last reply
      0
      • A Adan895

        I am struggling triying to accomodate my QTableView to make it easy to the user.

        This functions works as i really need:

        ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        The issue with this is that the headers are not user adjustable anymore, are completely frozzen.

        I know we also have this function which allows the user to adjust the Headers, but after using it, the headers go back as if i had not used QHeaderView::Stretch before:

        ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
        I have really looked for this on many websites with no luck of finding a understandable answer for my level.

        Thanks!

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

        @Adan895 said in QTableView Stretch Headers but keep Interactive:
        That is what https://doc.qt.io/qt-5/qheaderview.html#ResizeMode-enum says, Interactive (the user can resize) and Stretch (widths are set to fill view) are different from/incompatible with each other. So perhaps you will have to explain exactly what you mean by your "Stretch Headers but keep Interactive"?

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

          Hi and welcome to devnet,

          Are you triggering a resize to content size somehere in your code ?

          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
          1
          • JonBJ JonB

            @Adan895 said in QTableView Stretch Headers but keep Interactive:
            That is what https://doc.qt.io/qt-5/qheaderview.html#ResizeMode-enum says, Interactive (the user can resize) and Stretch (widths are set to fill view) are different from/incompatible with each other. So perhaps you will have to explain exactly what you mean by your "Stretch Headers but keep Interactive"?

            A Offline
            A Offline
            Adan895
            wrote on last edited by
            #4

            @JonB Hi, i want to Stretch the headers to the size of the QTableview, but allow the user to adjust them if necessary.

            JonBJ 1 Reply Last reply
            0
            • A Adan895

              @JonB Hi, i want to Stretch the headers to the size of the QTableview, but allow the user to adjust them if necessary.

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

              @Adan895
              So I think you should leave the mode on interactive and do a once-off stretch to get going?

              setSectionResizeMode(QHeaderView::Interactive);
              resizeSections(QHeaderView::Stretch);
              
              1 Reply Last reply
              0
              • SGaistS SGaist

                Hi and welcome to devnet,

                Are you triggering a resize to content size somehere in your code ?

                A Offline
                A Offline
                Adan895
                wrote on last edited by
                #6

                @SGaist Hello, no, i am just stretching the headers to the QTableview size using QHeaderView::Stretch, but if i do that the user cannot resize the Headers if necessary manually.

                Christian EhrlicherC JonBJ 2 Replies Last reply
                0
                • A Adan895

                  @SGaist Hello, no, i am just stretching the headers to the QTableview size using QHeaderView::Stretch, but if i do that the user cannot resize the Headers if necessary manually.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @Adan895 So why don't you do what @JonB suggested? Maybe use ResizeToContents depending on what you really 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
                  1
                  • A Adan895

                    @SGaist Hello, no, i am just stretching the headers to the QTableview size using QHeaderView::Stretch, but if i do that the user cannot resize the Headers if necessary manually.

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

                    @Adan895
                    Have you tried/noted the difference between setSectionResizeMode() and resizeSections(), as per the suggested code?

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      Adan895
                      wrote on last edited by Adan895
                      #9

                      @JonB @Christian-Ehrlicher @SGaist Thanks, what you are suggesting sounds logical, i think it will work, once i arrive home later i will try it and i ll let you know.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Adan895
                        wrote on last edited by Adan895
                        #10

                        @JonB @SGaist @Christian-Ehrlicher Hi guys, after triying using your advice, this is what happened, its not stretching. Captura de pantalla de 2020-01-08 19-14-52.png

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Adan895
                          wrote on last edited by Adan895
                          #11

                          Hi Guys, after Struggling like 3 hours ive got it:

                          for(int c = 0;c<=4;c++){
                                  ui->tableView->horizontalHeader()->resizeSection(c, 150);
                                  }
                          
                                  ui->tableView->horizontalHeader()->setStretchLastSection(true);
                          

                          Set first the width of each column to match the QTableView size and then Stretch the last section, thanks a lot!

                          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