How to detect moved columns
-
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());
-
@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.
-
@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.
-
@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(). -
@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. -
@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.
-
Christian Ehrlicher Lifetime Qt Championreplied to FumbleFingers on last edited by Christian Ehrlicher
@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.
-
@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).