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
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
- 2Then 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
|