Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Changing column order in a QTableView?
Qt 6.12 LTS is out! See what's new in the release blog

Changing column order in a QTableView?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 20.9k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #2

    Use a proxy model between your model and the view. To do this it looks like you may need to implement your own proxy since QSortFilterProxyModel sorts rows not columns.

    So subclass QAbstractProxyModel and reimplement the mapToSource() and mapFromSource() functions to swap those two columns around.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #3

      You can even reimplement them with QSFPM, so using this could be easier

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #4

        True and if other functionality of QSFPM is also required that would indeed be the way to go.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #5

          I would use a much simpler approach, without any subclassing.
          QTableView provides access to the header view via QTableView::horizontalHeader(). QHeaderView has a method called moveSection, that allows you to reorder the sections (in this case: columns) as you please.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #6

            Of course. I forgot about that method. ;-)

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on last edited by
              #7

              [quote author="Andre" date="1300615403"]I would use a much simpler approach, without any subclassing.
              QTableView provides access to the header view via QTableView::horizontalHeader(). QHeaderView has a method called moveSection, that allows you to reorder the sections (in this case: columns) as you please.

              [/quote]

              That's exactly what he's doing, so there's something else going on (wrong section numbers?)

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #8

                [quote author="peppe" date="1300626770"]
                [quote author="Andre" date="1300615403"]I would use a much simpler approach, without any subclassing.
                QTableView provides access to the header view via QTableView::horizontalHeader(). QHeaderView has a method called moveSection, that allows you to reorder the sections (in this case: columns) as you please.

                [/quote]

                That's exactly what he's doing, so there's something else going on (wrong section numbers?)[/quote]

                Not exactly, but close, you're right. scarleton, could you insert some debug statement to see the actual values of the sections you're trying to swap?

                André

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  scarleton
                  wrote on last edited by
                  #9

                  Well, I added extracted the values:

                  @productTableHeaderView->setMovable(true);
                  int priceVisualIdx = productTableHeaderView->sectionPosition(_priceListItemModel->priceNo());
                  int imageMaxVisualIdx = productTableHeaderView->sectionPosition(_priceListItemModel->imageMaxCntNo());
                  qDebug("priceVisualIdx: %d imageMaxVisualIdx: %d", priceVisualIdx, imageMaxVisualIdx);
                  productTableHeaderView->swapSections (imageMaxVisualIdx, priceVisualIdx);@

                  priceVisualIdx: 100 imageMaxVisualIdx: 300

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dangelog
                    wrote on last edited by
                    #10

                    So, it's likely that those values are actually the position in pixels of those sections. If
                    @
                    _priceListItemModel->priceNo()
                    _priceListItemModel->imageMaxCntNo()
                    @
                    are correct (i.e. they return the logical column index), you can pass them directly to swapSections.

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      scarleton
                      wrote on last edited by
                      #11

                      That was it, thank you!

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #12

                        I can imagine the swap did not work between column 100 and 300, in any way :-)

                        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