When setting the setSortingEnabled(true) instance of QTableView , switchable sorting icons appear in the column headers, but when you click on them, the data in the table itself does not occur. I understand that setting one setSortingEnabled(true) not enough? Need to override something else?

    1 answer 1

    Need to override something else?

    Of course. There, somewhere in the wilds of your code, describe the necessary slot that will sort (or provide sorting conditions), let's call it virtual void SlotSort(int iColumn) . Then in the widget where QTableView is located we connect the signal to the slot:

     connect(Table->horizontalHeader(),SIGNAL(sectionClicked(int)), this, SLOT(SlotSort(int))); 

    Where, Table - is QTableView*

    Simplified Sort Slot Example

     void Browse_Card::SlotSort(int iColumn) { // Π² классС запоминаСися Π½ΠΎΠΌΠ΅Ρ€ ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ сортировки SortColumn = iColumn; // Π² классС запоминаСися Π½Π°ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ сортировки SortOrder = Table->horizontalHeader()->sortIndicatorOrder(); // Π΄Π°Π»Π΅Π΅ собираСтся "хвост" SQL-запроса, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Ρ‚Π°ΠΊΠΆΠ΅ хранится Π² классС OrderBy = " ORDER BY "; if (Mode == bcService) { switch (iColumn) { case 3: OrderBy += "LOWER(f.\"Surname\") "; break; case 4: OrderBy += "LOWER(f.\"FirstName\") "; break; case 5: OrderBy += "LOWER(f.\"Patronymic\") "; break; case 6: OrderBy += "f.\"Age\" "; break; case 7: OrderBy += "LOWER(f.\"Reason\") "; break; case 8: OrderBy += "LOWER(f.\"Present\") "; break; default: OrderBy += "f.\"SortNum\" "; break; } } if (SortOrder == Qt::DescendingOrder) OrderBy += "DESC"; // Ρ‚ΡƒΡ‚ ΠΌΠΎΠΆΠ½ΠΎ Ρ„Π»Π°Π³ Π²Ρ‹ΡΡ‚Π°Π²ΠΈΡ‚ΡŒ, Π»ΠΈΠ±ΠΎ emit сигнала Π½Π° ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ NeedUpdate = true; } 
    • The meaning is clear. Thank. Well, if I do not need to sort the data in the model? It is enough for me to sort them in the view. - Artik
    • 2
      Then you need to use QSortFilterProxyModel ( doc.qt.io/qt-5/qsortfilterproxymodel.html ) - Majestio
    • But it helped. Thank you very much. - Artik
    • Nezshta) Put a "bird" on the question, so that it would be displayed as "solved", it would suddenly be useful for people. - Majestio