Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView header resizes when clicked
Qt 6.12 LTS is out! See what's new in the release blog

QTableView header resizes when clicked

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 3.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.
  • M Offline
    M Offline
    Mr Gisa
    wrote on last edited by
    #1

    I have a QTableView and I set the size of the headers using setColumnWidth and I also have the setSortingEnabled to true however if I click in any header the size of the columns is resized.

    How to keep the columns width even when the headers are clicked?

    JonBJ 1 Reply Last reply
    0
    • M Mr Gisa

      I have a QTableView and I set the size of the headers using setColumnWidth and I also have the setSortingEnabled to true however if I click in any header the size of the columns is resized.

      How to keep the columns width even when the headers are clicked?

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

      @Mr-Gisa
      If that really is the behviour you see, and if noone states how the widths can be preserved on new query (from header click), you can save the widths before the sort and restore afterward.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mr Gisa
        wrote on last edited by
        #3

        Sorry my ignorance but I really didn't understand what you said :(

        JonBJ 1 Reply Last reply
        0
        • M Mr Gisa

          Sorry my ignorance but I really didn't understand what you said :(

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

          @Mr-Gisa
          You have header click method which sets which column to sort by, causing the refresh?

          Before setting the sort column, read the current column widths via columnWidth() first, let it repopulate, and restore widths via setColumnWidth() correspondingly.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mr Gisa
            wrote on last edited by
            #5

            You have header click method which sets which column to sort by, causing the refresh?

            Not, it's only the QTableView with the setSortingEnabled as true, nothing more than that.

            ui->`tableView->setModel(mProxyModel);
            ui->`tableView->setMouseTracking(true);
            ui->`tableView->setSortingEnabled(true);
            ui->`tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
            ui->`tableView->setSelectionMode(QAbstractItemView::SingleSelection);
            ui->`tableView->horizontalHeader()->setStretchLastSection(true);
            ui->`tableView->verticalHeader()->hide();
            ui->`tableView->setColumnWidth(0, 230);
            ui->`tableView->setColumnWidth(1, 270);
            ui->`tableView->setColumnWidth(2, 270);
            ui->`tableView->setColumnWidth(3, 80);
            ui->`tableView->setColumnWidth(4, 80);
            ui->`tableView->setColumnWidth(5, 80);
            

            That is how I set the table up.

            If the table is empty and I click on the header it resizes the column width, it only doesn't resize when I have items in the table.

            How to prevent the header to resize if there's no item in it?

            JonBJ 1 Reply Last reply
            0
            • M Mr Gisa

              You have header click method which sets which column to sort by, causing the refresh?

              Not, it's only the QTableView with the setSortingEnabled as true, nothing more than that.

              ui->`tableView->setModel(mProxyModel);
              ui->`tableView->setMouseTracking(true);
              ui->`tableView->setSortingEnabled(true);
              ui->`tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
              ui->`tableView->setSelectionMode(QAbstractItemView::SingleSelection);
              ui->`tableView->horizontalHeader()->setStretchLastSection(true);
              ui->`tableView->verticalHeader()->hide();
              ui->`tableView->setColumnWidth(0, 230);
              ui->`tableView->setColumnWidth(1, 270);
              ui->`tableView->setColumnWidth(2, 270);
              ui->`tableView->setColumnWidth(3, 80);
              ui->`tableView->setColumnWidth(4, 80);
              ui->`tableView->setColumnWidth(5, 80);
              

              That is how I set the table up.

              If the table is empty and I click on the header it resizes the column width, it only doesn't resize when I have items in the table.

              How to prevent the header to resize if there's no item in it?

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

              @Mr-Gisa
              So I'm saying if, for whatever reason, even if it's a Qt bug, you want to know about a column click you can use something like https://forum.qt.io/topic/87849/how-do-i-detect-which-column-was-clicked-in-a-qtableview-header/5. And then do what I said about saving & restoring your desired column widths.

              If the table is empty and I click on the header it resizes the column width, it only doesn't resize when I have items in the table.

              That's probably a "feature". Qt table views don't seem too interested if they have no rows to show. Just an observation.

              You could even presumably use the above click-detection and not pass the signal on if your rows are empty, if that's the only case where it goes wrong and you don't care in that case about doing the sorting.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mr Gisa
                wrote on last edited by
                #7

                Thanks, I did the way you said and it's working fine.

                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
                • Unsolved