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 detect moved columns
QtWS25 Last Chance

How to detect moved columns

Scheduled Pinned Locked Moved Unsolved General and Desktop
proxyviewcolumnsc++
8 Posts 3 Posters 1.0k Views
  • 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.
  • F Offline
    F Offline
    FumbleFingers
    wrote on last edited by FumbleFingers
    #1

    I'm running the standard free version of QT Creator 7.0.0 under Linux Mint 20, within which the demo program illustrating my problem is...

    /opt/Qt/Examples/Qt-6.2.2/widgets/itemviews/ basicsortfiltermodel

    ...where I added this line to function void Window::sortChanged() ...

    printf("%s\n",proxyModel->headerData(1,Qt::Horizontal,Qt::DisplayRole).toString().toStdString().c_str());
    

    This prints Sender (second column heading of 3, subscripted 0,1,2) every time I toggle the Case sensitive sorting checkbox in bottom right of the display.

    BUT after I drag the Sender column heading to the right of the third column (headed Date), I would expect it to print Date, because that is now column 2, not column 3.

    It makes no difference if I drag the columns into a different sequence on the upper Original Model or the lower Sorted/Filtered Model part of the display.

    I've presented the issue the simplest way I can. Obviously I have an actual application derived from the example, where things are a bit more complicated. So I should also mention that I've been using the following function to access an ID value stored in column 5 (subscript 4) in my program...

    ((QSortFilterProxyModel*)proxyModel)->data(proxyView->currentIndex().siblingAtColumn(4));
    

    ...which still retrieves the correct ID value after I drag headings around so that ID isn't column 5 any more.

    How can I detect that columns have been moved? Ideally I just want to query the system to ask what the final headings are when the program ends. I really hope I don't need to override default methods and/or process low-level mouse "drag & drop" signals, because I'm not very good at things like that.

    EDIT:

    int vi=proxyView->header()->visualIndex(1);
    printf("%s\n",proxyModel->headerData(vi,Qt::Horizontal,Qt::DisplayRole).toString().toStdString().c_str());
    
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • F FumbleFingers

      I'm running the standard free version of QT Creator 7.0.0 under Linux Mint 20, within which the demo program illustrating my problem is...

      /opt/Qt/Examples/Qt-6.2.2/widgets/itemviews/ basicsortfiltermodel

      ...where I added this line to function void Window::sortChanged() ...

      printf("%s\n",proxyModel->headerData(1,Qt::Horizontal,Qt::DisplayRole).toString().toStdString().c_str());
      

      This prints Sender (second column heading of 3, subscripted 0,1,2) every time I toggle the Case sensitive sorting checkbox in bottom right of the display.

      BUT after I drag the Sender column heading to the right of the third column (headed Date), I would expect it to print Date, because that is now column 2, not column 3.

      It makes no difference if I drag the columns into a different sequence on the upper Original Model or the lower Sorted/Filtered Model part of the display.

      I've presented the issue the simplest way I can. Obviously I have an actual application derived from the example, where things are a bit more complicated. So I should also mention that I've been using the following function to access an ID value stored in column 5 (subscript 4) in my program...

      ((QSortFilterProxyModel*)proxyModel)->data(proxyView->currentIndex().siblingAtColumn(4));
      

      ...which still retrieves the correct ID value after I drag headings around so that ID isn't column 5 any more.

      How can I detect that columns have been moved? Ideally I just want to query the system to ask what the final headings are when the program ends. I really hope I don't need to override default methods and/or process low-level mouse "drag & drop" signals, because I'm not very good at things like that.

      EDIT:

      int vi=proxyView->header()->visualIndex(1);
      printf("%s\n",proxyModel->headerData(vi,Qt::Horizontal,Qt::DisplayRole).toString().toStdString().c_str());
      
      
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @FumbleFingers said in How to detect moved columns:

      How can I detect that columns have been moved?

      Even though I don't understand your problem you should take a look at QHeaderView::visualIndex() and QHeaderView::logicalIndex(). Dragging the header around only changes the visual index, not the logical one to not screw up the model stuff.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      F 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @FumbleFingers said in How to detect moved columns:

        How can I detect that columns have been moved?

        Even though I don't understand your problem you should take a look at QHeaderView::visualIndex() and QHeaderView::logicalIndex(). Dragging the header around only changes the visual index, not the logical one to not screw up the model stuff.

        F Offline
        F Offline
        FumbleFingers
        wrote on last edited by
        #3

        @Christian-Ehrlicher This is my first time of using this site, so I'm still struggling a bit with the user interface. My problem is exactly and only that I want to be able to detect the "final" left-to-right sequence of my columns, given that it's possible for a user to drag columns left and right to change the display. I just want to remember any changed sequence for next time the app runs.

        JonBJ 1 Reply Last reply
        0
        • F FumbleFingers

          @Christian-Ehrlicher This is my first time of using this site, so I'm still struggling a bit with the user interface. My problem is exactly and only that I want to be able to detect the "final" left-to-right sequence of my columns, given that it's possible for a user to drag columns left and right to change the display. I just want to remember any changed sequence for next time the app runs.

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

          @FumbleFingers
          I think you will find @Christian-Ehrlicher's reply is the key. Have you clicked on those two links, read and understood visual vs logical indexes?

          For what you want to save, sounds like you want to iterate through the visual index order saving the column number or name.

          Does this example use QTableView? You might be interested in https://stackoverflow.com/a/19062154/489865 and QHeaderView::saveState().

          F 1 Reply Last reply
          2
          • JonBJ JonB

            @FumbleFingers
            I think you will find @Christian-Ehrlicher's reply is the key. Have you clicked on those two links, read and understood visual vs logical indexes?

            For what you want to save, sounds like you want to iterate through the visual index order saving the column number or name.

            Does this example use QTableView? You might be interested in https://stackoverflow.com/a/19062154/489865 and QHeaderView::saveState().

            F Offline
            F Offline
            FumbleFingers
            wrote on last edited by
            #5

            @JonB: Thank you guys so much! I've already established using the trivially modified basicsortfiltermodel that QHeaderView::logicalIndex returns me the revised (after "dragging") column numbers, which I think is all I actually need.
            Hopefully QHeaderView::saveState and restoreState will transparently handle any user column shifting for me. If not I think I could save the logical index values myself, and set up columns in a potentially revised sequence on program startup.
            I will mark this second response here as "solving" my problem partly because of the useful StackExchange link that mentions saveState (which I didn't know about, although I did realise I might want something like that). But you've both been super helpful, thanks.

            JonBJ Christian EhrlicherC 2 Replies Last reply
            0
            • F FumbleFingers

              @JonB: Thank you guys so much! I've already established using the trivially modified basicsortfiltermodel that QHeaderView::logicalIndex returns me the revised (after "dragging") column numbers, which I think is all I actually need.
              Hopefully QHeaderView::saveState and restoreState will transparently handle any user column shifting for me. If not I think I could save the logical index values myself, and set up columns in a potentially revised sequence on program startup.
              I will mark this second response here as "solving" my problem partly because of the useful StackExchange link that mentions saveState (which I didn't know about, although I did realise I might want something like that). But you've both been super helpful, thanks.

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

              @FumbleFingers said in How to detect moved columns:

              that QHeaderView::logicalIndex returns me the revised (after "dragging") column numbers, which I think is all I actually need

              This would surprise @Christian-Ehrlicher and me :) The logical indexes should be unaffected by "dragging columns around in the view/UI". We would expect it to be the visual indexes only which are "revised"! But I haven't looked at the example or how your code works. But if you have what you want that's fine.

              F 1 Reply Last reply
              0
              • F FumbleFingers

                @JonB: Thank you guys so much! I've already established using the trivially modified basicsortfiltermodel that QHeaderView::logicalIndex returns me the revised (after "dragging") column numbers, which I think is all I actually need.
                Hopefully QHeaderView::saveState and restoreState will transparently handle any user column shifting for me. If not I think I could save the logical index values myself, and set up columns in a potentially revised sequence on program startup.
                I will mark this second response here as "solving" my problem partly because of the useful StackExchange link that mentions saveState (which I didn't know about, although I did realise I might want something like that). But you've both been super helpful, thanks.

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

                @FumbleFingers said in How to detect moved columns:

                that QHeaderView::logicalIndex returns me the revised (after "dragging") column numbers, which I think is all I actually need.

                No, they don't change when you move the header items around. The logical index is always fixed, the visualIndex will change.

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

                  @FumbleFingers said in How to detect moved columns:

                  that QHeaderView::logicalIndex returns me the revised (after "dragging") column numbers, which I think is all I actually need

                  This would surprise @Christian-Ehrlicher and me :) The logical indexes should be unaffected by "dragging columns around in the view/UI". We would expect it to be the visual indexes only which are "revised"! But I haven't looked at the example or how your code works. But if you have what you want that's fine.

                  F Offline
                  F Offline
                  FumbleFingers
                  wrote on last edited by
                  #8

                  @JonB My EDIT revision now prints the column heading I wanted. Hopefully I only need to run that header saveState function just before program exit, and restoreState just before displaying my table on the next run. If that does what I want, I won't need to directly deal with "visual indexes" at all at this stage in my ongoing "personal movie database" project.
                  Once I can persistently re-sequence columns, I hope to add several more columns, with the less interesting ones dragged to the right of the row out of the way. I already remember user-revised column widths, so I can make those rightmost columns as narrow as possible, but I fully expect to progress to optionally hiding temporarily unwanted columns.
                  Anyway, I'm getting older and slower, so it'll probably take me a few days just to implement "moved column memory" in my app. But eventually I want several members of a "cinema club" each having read/write access to their own database (on DropBox, GoogleDrive, whatever), and read-only access to the other members' databases (to retrieve "rating" and "date seen" values by user).

                  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