如何在QAbstractItemModel和我的std :: map<>之间创建连接数据?

时间:2011-08-15 13:09:45

标签: qt4 stdmap qabstractitemmodel

我有一些数据,例如

Company Name | Person Field
-----------------------
(Company1)   | [Company1 Descr]
     L       | [Person1]
(Company2)   | [Company2 Descr]
     L       | [Person2]
     L       | [Person3]
(Company3)   | [Company3 Descr]
     L       | [Person4]
(Company4)   | [Company4 Descr]
     L       | [Person5]

这是一个std::map< CompanyPtr, PersonPtr >。我希望将此mapQAbstractItemModel相符,然后将其与QTreeView相关联。

我使用了示例Simple Tree Model,但它很复杂(这里我说的是TreeItem类和setupModelData方法。)

这是我的模特课:

class ContractorModel : public QAbstractItemModel
{
     Q_OBJECT

     // types
     enum Position
     {
            PositionCompany = 0
          , PositionContractor
          , ColumnCount
     };

     typedef   std::map< CompanyPtr, ContractorPtr >   ComConMap;
     typedef  std::pair< CompanyPtr, ContractorPtr >   ComConPair;

     // methods
     void _init();

public:
     ContractorModel( QObject* parent = 0 );
     ~ContractorModel();

     QVariant data( const QModelIndex &index, int role /* = Qt::DisplayRole */ ) const;
     QVariant headerData( int section, Qt::Orientation orientation, int role /* = Qt::DisplayRole*/ ) const;

     QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
     QModelIndex parent( const QModelIndex &index ) const;

     int rowCount( const QModelIndex &parent /* = QModelIndex */ ) const;
     int columnCount( const QModelIndex &parent /* = QModelIndex */ ) const;

signals:
     void resizeToContents();

public slots:
     void refresh();
     void refreshData();

protected:
private:
     ContractorItem* rootItem;
     ComConMap buddies_ccm_;
};

在我的课堂上,我无法正确实现这两种方法:

QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
QModelIndex parent( const QModelIndex &index ) const;

也许有人已经解决了这个问题?

谢谢!

0 个答案:

没有答案