3

Qt 클래스를 사용하여 특정 데이터 모델을 사용하는 C++ 응용 프로그램을 작성하고 있습니다. 이를 위해 나는 QAbstractItemModel에서 상속 :Qt에서 QModelIndex의 직계 자식 색인을 정렬하는 방법

// the following is a class that represents the actual data used in my application 
class EventFragment 
{ 
.... 
private: 
    qint32 address; 
    QString memo; 
    QDateTime dateCreated; 
    QVector<EventFragment*> _children; 
.... 
}; 

// the following is the model representation that used by my application to display the actual details to the user 
class EventModel : public QAbstractItemModel 
{ 
    Q_OBJECT 
public: 
    explicit EventModel (QObject *parent = 0); 
.... 
private: 
    // the following is the root item within the model - I use a tree-like presentation to show my data 
    EventFragment* _rootFragment; 
}; 

를 내 응용 프로그램에서 필터/정렬 옵션을 필요로 어떤 시점에서 내가 또한 정렬을 달성하기 위해 QSortFilterProxyModel

class EventProxyModel : public QSortFilterProxyModel 
{ 
    Q_OBJECT 
public: 
    explicit EventProxyModel (QObject *parent = 0); 
... 
public: 
    // I had to add my custom implementation in the 'lessThan' method to achieve a 
    // more complex sort logic (not just comparing the actual values but using 
    // additional conditions to compare the two indexes) 
    virtual bool lessThan (const QModelIndex & left, const QModelIndex & right) const; 
... 
}; 

에서 상속하는 클래스를 생성, 나는 기본 QSortFilterProxyModel::sort() 메서드를 사용했습니다 (프록시 모델 클래스에서 다시 구현하지 않았습니다). 그리고 시간이 지나면 작동하는 것 같았습니다.

어떤 점에서는 실제 QSortFilterProxyModel::sort() 메서드가 전체 모델을 정렬하고 특정 인덱스의 직접적인 자식 만 정렬하는 것이 필요하다는 것을 알았습니다.

EventModel 클래스의 sort() 메서드를 다시 구현하려고했지만 잠시 후 QSortFilterProxyModel::sort()은 전혀 언급하지 않는다는 것을 깨달았습니다. 반면에 인덱스를 안전하게 재구성하여 모델을 표시하는 뷰가 충돌하지 않도록하는 방법을 모르겠습니다.

는 난 단지 특정 QModelIndex의 직계 자식을 정렬하는 방법이 있어야합니다 생각하지만, 나는 아직 그것을 발견하지 않았습니다.

제 사례에 대한 가능한 해결책 또는 그것을 수행하는 방법에 대한 지침을 보여주는 자습서/예가 있습니까?

감사

답변

1

당신이 정렬하지 않는 인덱스에 대해 전혀 비교하지 않는 최적의 솔루션을 원하는 경우에, 당신이 아닌 당신의 자신의 QAbstractProxyModel을 reimeplement 할 거라고 생각 - 사소한 일. 가 아닌 최적화 된 솔루션으로 괜찮아요 경우, 나는이 시도 것 :

bool EventProxyModel::lessThan(const QModelIndex & left, const QModelIndex & right) const { 
    if (left.parent() == isTheOneToSortChildrenFor) { 
     ...apply custom comparison 
    } else { 
     return left.row() < right.row(); 
    } 
} 

을있는 그대로 특정 부모와 함께 다음 인덱스를 다른 모든 것을 남겨 두어야 소스의 행을 비교.