我有型QStandardItemModel的模型,看起來像這樣:排序QStandardItemModel在C++的Qt
QHash<int, QByteArray> roleNames;
roleNames[Car2goVehicle::NameRole] = "plate_number";
roleNames[Car2goVehicle::DescriptionRole] = "address";
roleNames[Car2goVehicle::FuelRole] = "fuel";
roleNames[Car2goVehicle::InteriorRole] = "interior";
roleNames[Car2goVehicle::ExteriorRole] = "exterior";
roleNames[Car2goVehicle::VinRole] = "vin";
roleNames[Car2goVehicle::LatRole] = "lat";
roleNames[Car2goVehicle::LonRole] = "lon";
roleNames[Car2goVehicle::DistanceRole] = "distance";
d->m_vehiclesmodel = new RoleItemModel(roleNames);
,現在我想根據這樣
d->m_vehiclesmodel->setSortRole(Qt::UserRole);
d->m_vehiclesmodel->sort(Car2goVehicle::DistanceRole, Qt::AscendingOrder);
距離進行排序,但結果是錯誤的。有人能告訴我如何排序嗎?
謝謝。
您甚至可以說,無論您如何對模型進行排序,視圖可能會選擇以其他方式顯示它。這就是爲什麼你需要使用'QSortFilterProxyModel'。 – RedX 2011-04-20 14:33:00
我也使用過QSortFilterProxyModel,但我的模型根據Car2goVehicle :: NameRole排序,而不是Car2goVehicle :: DistanceRole – Temirlan 2011-04-21 13:16:32
Termirlan:您是否將角色與列混淆?我在上面的代碼片段中添加了一段。 – 2011-04-21 15:25:44