2014-11-23 53 views
0

有一個簡單的辦法有一個QListWidget第一顯示所有檢查其持有的項目,然後將所有未覈對的,既塊按字母順序排序?因此,新檢查的項目會自動置於正確位置的「已檢查」塊,反之亦然?排序項由他們選中狀態

感謝:-)

回答

0

最簡單的方法都建議可能是創建自己的QListWidgetItem派生類,以替代實現,低於運營商。即,沿着線的東西:

class MyQListWidgetItem : public QListWidgetItem { 
    public: 
     // TODO: provide implementation of constructors 

     virtual bool operator< (const QListWidgetItem & other) const { 
      // if check state differs, use the difference for comparison 
      if(checkState() != other.checkState()) 
       // Qt::Checked = 2 and Qt::Unchecked = 0 -> to get checked items first, 
       // we have to "revert" the comparison operation 
       return checkState() > other.checkState(); 

      // otherwise just return the comparison result from the base class 
      return QListWidgetItem::operator < (other); 
     } 
} 

就用這個新品種類將所有新項目到你的列表中,它應該結束了有序的,因爲你需要它。

爲了確保在用戶更改某個項目的狀態後列表仍被排序,您還應在列表中創建一個插槽,該插槽在觸發QListWidget::itemChanged()時運行QListWidget::sortItems()

+0

非常感謝,我會試試這個! – 2014-11-23 21:35:19

+0

酷!請接受我的答案,如果它的工作:) – 2014-11-24 09:17:59