2010-06-23 5 views
0

사진, 아이콘 및 경로 이름에 관한 데이터를 저장할 모델을 만들려고합니다.커스텀 Qt ItemModel에 문제가 있습니다

class PhotoListItemModel : public QAbstractItemModel { 
    struct ItemModelType { 
     std::string fileName; 
     QImage  image; 
     boost::shared_ptr<char> unique_id; 
    }; 
    std::map<string, ItemModelType> ItemMap; 
    std::map<char*, string>   ItemPointerMap; 
    std::deque<char*>    ItemIndexMap; 

public: 
    PhotoListItemModel(QObject* parent); 
    virtual bool   clear(); 
    virtual int   rowCount (const QModelIndex & parent = QModelIndex()) const; 
    virtual QVariant  data (const QModelIndex & index, int role = Qt::DisplayRole) const; 
    virtual QModelIndex parent (const QModelIndex & index) const; 
    virtual QModelIndex index (int row, int column, const QModelIndex & parent = QModelIndex()) const; 
    virtual int   columnCount (const QModelIndex & parent = QModelIndex()) const; 
    //virtual QMap<int, QVariant> itemData (const QModelIndex & index) const 
    virtual bool removeColumns (int column, int count, const QModelIndex & parent = QModelIndex()); 
    virtual bool removeRows (int row, int count, const QModelIndex & parent = QModelIndex()); 

    //index(), parent(), rowCount(), columnCount(), and data() 
    int addFile(const string& str, const QImage& img); 
    bool removeItem(const QModelIndex&); 
}; 

PhotoListItemModel::PhotoListItemModel(QObject* parent) :  QAbstractItemModel(parent) { 

} 
bool PhotoListItemModel::removeItem(const QModelIndex& idx) { 
    return(false); 
} 
bool PhotoListItemModel::removeColumns (int column, int count, const QModelIndex & parent) { 
    return false; 
} 
bool PhotoListItemModel::removeRows (int row, int count, const QModelIndex & parent) { 
    return false; 
} 
int PhotoListItemModel::rowCount (const QModelIndex & parent) const { 
    return 1; 
} 
bool PhotoListItemModel::clear() { 
    return true; 
} 
QVariant  PhotoListItemModel::data (const QModelIndex & index, int role) const { 
    if (!index.isValid()) 
     return QVariant(); 

    if (role == Qt::TextAlignmentRole) { 
     return int(Qt::AlignCenter); 
    } else if (role == Qt::DisplayRole) { 
     char* val = ItemIndexMap[index.column()]; 
     const map<char*, string>::const_iterator iterPtr = ItemPointerMap.find(val); 
     const map<string, ItemModelType>::const_iterator iterImg = ItemMap.find(iterPtr->second); 
     const QImage &img = iterImg->second.image; 

     return img; 
    } 
    return QVariant(); 
} 
QModelIndex PhotoListItemModel::parent (const QModelIndex & index) const { 
    return QModelIndex(); 
} 
QModelIndex PhotoListItemModel::index (int row, int column, const QModelIndex & parent) const { 
    char* ptr = ItemIndexMap[column]; 
    return createIndex(row, column, ptr); 
} 
int PhotoListItemModel::columnCount (const QModelIndex & parent) const { 
    int colCount = ItemMap.size(); 
// if (colCount < 3) 
//  colCount = 3; 

    return colCount; 
} 

int PhotoListItemModel::addFile(const string& str, const QImage& img) { 
    ItemModelType itype; 
     itype.fileName = str; 
     itype.image = img; 
     itype.unique_id = boost::make_shared<char>(); 

    ItemMap[str] = itype; 
    ItemPointerMap[itype.unique_id.get()] = str; 
    ItemIndexMap.push_back(itype.unique_id.get()); 

    int column = ItemIndexMap.size() - 1; 
    QModelIndex mIndex = createIndex(0, column, ItemIndexMap[column]); 
    emit dataChanged(mIndex, mIndex); 

    beginInsertColumns(QModelIndex(), columnCount()-1 , columnCount()-1); 
    bool ret = this->insertColumn(columnCount()-1); 
    endInsertColumns(); 
} 

Qt 엔진은 columnCount() 횟수만큼 rowCount를 여러 번 호출합니다. 위젯 클래스는 addFile()을 6 회 호출합니다.

PhotoListItemModel :: data()가 호출되지 않기 때문에 Qt가 내가 변경 한 내용을 듣지 못하거나 뭔가를 놓치고 있습니다. 예를 들어, columnCount를 6으로 설정하면 :: 데이터가 호출됩니다. (그리고 QImages가 표시되는지 확인하지 않았습니다.) 한 번에 한 가지.

궁극적으로 저는 이것을 ListView에 연결하여 사진 축소판을 표시합니다 첫째.

답변

1

, 나는 당신이 다음 parent()index() 구현을 제거 할 수 있습니다. 혼자 그 변화가 모든 문제를 해결할 수 있습니다. 당신이 QAbstractItemModel 대신, QAbstractTableModel을 상속 시도해야한다고 생각, 모든 기본의 자동 처리됩니다 당신을위한 물건. 나는 Qt가 그 것처럼 동작 할 것임을 발견했다. QModelIndex 나는 그것을 전달한 잘못된 속성이 true로 설정되어있어 귀하의 케이스 인 것으로 보인다.

+0

다음과 같이 calltree가 변경되었습니다. 여기 calltree는 columnCount(), columnCount(), columnCount(), rowCount(), addFile(), rowCount(), columnCount(), rowCount(), columnCount 4. data()에 대한 호출이 없습니다. –

+0

QStandardItem :: setData에 내 데이터 유형 ItemModelType에 대한 포인터를 전달하는 것이 더 좋을까요? –

+0

setData가 답이라고 생각하지 않습니다. 가장 최근 변경 사항으로 게시 한 코드를 업데이트 해보십시오. 여기에서 살펴 보겠습니다. – Gianni

0

모델 생성자에서 insertRows(0, rowCount());을 호출 해 보았습니까? 그것은 항상 나를 위해 잘 작동하는 것 같습니다.

+0

왜 작동합니까? 생성자에서 모델은 여전히 ​​비어 있습니다. –

관련 문제