如何用Qt对持续时间进行排序?
我正在尝试使 QSortFilterProxyModel 按持续时间对项目进行排序。
前提条件:
- 小时和分钟不能有前导零,
- 如果持续时间小于一小时,则不能显示小时,但只有分钟和秒,
- 如果持续时间小于一分钟,则必须显示 0 分钟和相应的秒数[0:42]
尝试将源模型中的持续时间存储为 H:mm:ss ( http://doc.trolltech.com/4.6/qtime.html#toString) 如果它们是一小时或更长,则为 m:ss - 如果少于一小时,但由于 QString 的排序是按字母顺序排列的,那么,例如,5:20 比 12:09 “多”,因为它的第一个数字更大。
有没有一种优雅的方式来进行排序?
i'm trying to make QSortFilterProxyModel sort items by duration.
preconditions:
- hours and minutes must not have leading zeros
- if duration is less than an hour, then hours must not be displayed, but only minutes and seconds
- if duration is less than a minute, then 0 minutes and the corresponding number of seconds must be displayed [0:42]
tried to store durations in the source model as H:mm:ss (http://doc.trolltech.com/4.6/qtime.html#toString) if they are one hour or longer, and as m:ss - if less than an hour, but since sorting of QStrings is alphabetical, then, for instance, 5:20 is "more" than 12:09 as its first digit is greater.
is there an elegant way to do the sorting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过这种方法,您可以用您的方法显示 QTime 数据,并正确排序。
By this approach, you can display your QTime data with your approach, and sort correctly.
我对已接受答案的实现[DurationDelegate是QStyledItemDelegate的子类]:
my implementation of the accepted answer [DurationDelegate is a subclass of QStyledItemDelegate]: