2011-09-12 4 views
1

중복 된 값을 강조 표시하는 메커니즘을 구현해야합니다. 값은 값 유형에 따라 델리게이트를 통해 편집됩니다 (문자열 - 줄 편집, 길고 큰 10 진수 - 스핀 상자). 현재 모든 값과 개수를 두 개의 "병렬"목록에 저장하는 추가 클래스의 도움으로이 기능을 구현했습니다. 그리고 새 값을 추가 한 후에는 카운트 수를 늘립니다 (반복되는 값을 제거하면 감소합니다). 그러나이 솔루션은 너무 부피가 큰 것처럼 보입니다. 너희들은 setModelData(...) QItemDelegate의 방법에 강조 표시에 대한 다른 아이디어가 있습니까?Qt : QListWidget에서 중복 된 항목을 강조 표시하려면 어떻게합니까? (qtjambi)

/** 
* Stores a delegates' existing values 
*/ 
private final class DelegateValuesStorage { 
    private final List<Object> values = new ArrayList<Object>(); 
    private final List<Integer> counts = new ArrayList<Integer>(); 

    .... 

    //Add value or increase a count if exists 
    public void add(final Object value) { 
     if(values.contains(value)) { 
      final int valueIndex = values.indexOf(value); 
      final int oldCount = counts.get(valueIndex); 
      counts.remove(valueIndex); 
      counts.add(valueIndex, oldCount + 1); 
     } else { 
      values.add(value); 
      counts.add(1); 
     } 
    } 

    .... 

    //Decrease a count or remove value if it doesn't exist anymore 
    public void decreaseCount(final Object value) { 
     if(value == null) { 
      return; 
     } 
     final int index = values.indexOf(value); 
     if(index >= 0) { 
      final int oldCount = counts.get(index); 
      if(oldCount >= 2) { 
       counts.remove(index); 
       counts.add(index, oldCount - 1); 
      } else { 
       values.remove(index); 
       counts.remove(index); 
      } 
     } 
    } 

/** 
* Delegate 
*/ 
private class ConcreteDelegate extends QItemDelegate { 

    private final DelegateValuesStorage values = new DelegateValuesStorage(); 

    ... 

    @Override 
    public void setModelData(final QWidget editor, final QAbstractItemModel model, final QModelIndex index) { 
     if(editor instanceof ValEditor) { // ValEditor is an abstraction of line edit and spin box over values' data types 

      final Object value = ((ValEditor) editor).getValue(); 
      model.setData(index, value, Qt.ItemDataRole.UserRole); 
      final String newData = (value == null) ? "" : String.valueOf(value); 
      values.add(newData); 
      final String oldData = (String) model.data(index, Qt.ItemDataRole.DisplayRole); 
      values.decreaseCount(oldData); 


      model.setData(index, newData, Qt.ItemDataRole.DisplayRole); 
      model.setData(index, new QColor(0, 0, 0), Qt.ItemDataRole.ForegroundRole); 
      redrawItems(model); // runs through values and colors them red if count >= 2; or black if count == 1 

     } else { 
      super.setModelData(editor, model, index); 
     } 
    } 
} 

답변

3

나는 보통 작업의 그 종류의 맵을 사용 :

values.get(value) > 1 

true 경우 지금 강조

private final class DelegateValuesStorage { 
    private final Map<Object, Integer> values = new HashMap<Object, Integer>(); 

    .... 

    //Add value or increase a count if exists 
    public void add(final Object value) { 
     Integer count = values.get(value); 
     if (count == null) { 
      values.put(value, 1); 
     } else { 
      values.put(value, count + 1); 
     } 
    } 

    .... 

    //Decrease a count or remove value if it doesn't exist anymore 
    public void decreaseCount(final Object value) { 
     if(value == null) { 
      return; 
     } 
     Integer count = values.get(value); 
     if (count == null) { 
      // decreasing a "new" value - could be an error too 
      return; 
     } 
     if (count <= 1) { 
      // remove the value from the map 
      values.remove(value); 
     } else { 
      values.put(value, count - 1); 
     } 
    } 
} 

이 활성화되어야한다.