Sort Model, Select Multi Header Column !



  • Is there a way to select multiple Hedder Columns from the QTableView SortModel at the same time?



  • Not on the view but it can be done on the model.

    If your model does not use insert/remove columns you can:

    • use this proxy
    • call proxyModel->addSortPriority to select the order of the multiple sorts
    • call proxyModel->sort(i); (i being any number) to sort


  • @VRonin
    I have 3 observations/questions on your code:

    1. You may or may not care, but for published work I suspect your revoveSortPriority is mis-spelt and shold be removeSortPriority?

    2. I don't think your code allows the sorting to be in a different (default) direction per each column (e.g. my sort order requires column 2 to be descending when my column 2 is sorted ascending, like SQL ORDER BY col1 ASC, col2 DESC)?

    3. I don't understand what the i parameter to the sort is for in this situation?

    For #2 & #3, I may be misunderstanding the OP's question/your code. I thought this is about "how can I sort by multiple columns" (like I believe you can do in Excel?), so for example my column 1 may contain the same values in multiple rows, I then want it sub-sorted by column 2.



  • @JonB said in Sort Model, Select Multi Header Column !:

    You may or may not care, but for published work I suspect your revoveSortPriority is mis-spelt and shold be removeSortPriority?

    Yup

    I don't think your code allows the sorting to be in a different (default) direction per each column (e.g. my sort order requires column 2 to be descending when my column 2 is sorted ascending, like SQL ORDER BY col1 ASC, col2 DESC)?

    That's a very good observation, that code was a "quick solution" drafted for another user, I'm planning on making that a "real" thing and I added this to the things to implement: https://github.com/VSRonin/QtModelUtilities/issues/4

    I don't understand what the i parameter to the sort is for in this situation?

    QAbstractItemModel::sort requires a number, the proxy model then just ignores that number so i can be anything, the result won't change

    for example my column 1 may contain the same values in multiple rows, I then want it sub-sorted by column 2

    proxyModel->addSortPriority(1);
    proxyModel->addSortPriority(2);
    proxyModel->sort(0);
    


  • @VRonin

    QAbstractItemModel::sort requires a number, the proxy model then just ignores that number so i can be anything, the result won't change

    Ah, I didn't understand that is what you meant.

    You shouldn't do it that way, should you [politely]? It feels all wrong to me. If it were me, I think I would:

    1. Provide a new sort() method which takes no column parameter (assuming you can do that in C++, I can't in Python), and that does your sort.

    2. If you have to provide an override for sort(int column), the only one which would make sense to me would go like:

    if (column == m_sortPriority[0].first)
        return sort();
    else
        throw "Bad argument value";
    
    1. Your code allows specification of Qt::ItemDataRole for each function on each individual column in the multi-sort, and stores it in the QPair list element. This seems strange to me. The specification of a role should be shared by the values of all columns in the sort, e.g. I want them all for Qt::DisplayRole or all for Qt::EditRole in a given sort. Then you could drop storing the Qt::ItemDataRole in the QPair and use it to store the "natural direction" for each column's sort order which we discussed above.

    Please don't take any of this as criticism or arrogance! They're just my thoughts for discussion :)



  • @JonB said in Sort Model, Select Multi Header Column !:

    Provide a new sort() method which takes no column parameter (assuming you can do that in C++

    You can and I agree. The view will still call sort(int) though

    If you have to provide an override for sort(int column), the only one which would make sense to me

    Agree but I'd rather not override it at all, I can't think of a strictly better implementation than that of QSortFilterProxyModel::sort.

    Your code allows specification of Qt::ItemDataRole for each function on each individual column

    I still stick to my implementation. Say you want to sort the first column by the displayed column and the second column by some property you saved in Qt::UserRole

    Please don't take any of this as criticism or arrogance! They're just my thoughts for discussion :)

    Don't worry, I apreciate the discussion and the points you raise are more than reasonable



  • @VRonin said in Sort Model, Select Multi Header Column !:

    I still stick to my implementation. Say you want to sort the first column by the displayed column and the second column by some property you saved in Qt::UserRole

    Then (to me) you are going to have problems deriving from the base QSortFilterProxyModel Class. What are you going to do about properties like http://doc.qt.io/qt-5/qsortfilterproxymodel.html#sortRole-prop ?



  • Exactly the same thing I do with the column argument passed to sort. i ignore it as addSortPriority allows you a more generic solution



  • @VRonin
    Then we just have a difference of opinion on the approach to use for these kinds of methods. Which is fine, as I'm sure you have your reasons :)

    Incidentally, I just also noticed, your class is named MultySortProxy when the usual spelling would be MultiSortProxy. Might be a thought if you ever change revoveSortPriority, though I appreciate it would mean changing the class name.

    At which point, I shall shut up now. It has been interesting looking at your code, thank you.



  • @VRonin

    In the sorting model, I want to select multiple headers like Excel.

    In an alignment model, only one header is clicked or dragged.



  • @Pada_ said in Sort Model, Select Multi Header Column !:

    like Excel.

    Can you explain step-by-step how to obtain what you want in excel, I'll then "translate" it to Qt



  • @VRonin

    Like the Excel headers (A, B, C, and D), you want to select multiple header columns in Qt's list model.

    However, when using the sort model in the Qt list model, a triangle appears next to the header column, and only one header column can be selected.

    My Qt list model has five header columns. I want to use sort for header columns 1 through 3, and the rest 4 through 5 like to be able to select multiple header columns like a regular list model.



  • @Pada_
    You have to do this work yourself.

    Apart from @VRonin's kind suggestion of his own skeleton code, Googling for qtableview sort multiple columns will give you several solutions to look at.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.