亲身体会:一种需要取得子类详细信息的情况(五种方法)

void MyProgressBar::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{  
        // -------------------- 取数据,测试数据 ---------------------------------
        const QAbstractItemModel *item_model = index.model(); // 根据当前索引取得模型(包括了无数信息)
        int nRow = index.row(); // 当前行(每次的当前行)
        // 注意,这个index.model是QT库自带的函数:http://doc.qt.io/qt-4.8/qmodelindex.html
        // 它当然只能返回一个QAbstractItemModel的指针,而不是经过我继承和改造的Model,这样一来它就无法取得我添加的额外信息
        // 这个时候,必须要把这个model下降为子类才能准确取得需要的信息,大概有5种方法
        // 1.要么使用dynamic_cast,2.要么进行RTTI,3.要么强行转换(相当于static_cast),4.要么在MyProgressBar里提前记录我新定义的Model的类型指针,5.从全局变量中取得指针,6.不知道QT有没有额外的方法?

        // 之所以发生这种问题的根本原因在于,这个类库不是自己写的,它所提供的函数只能取得父类指针
}

 

posted @ 2016-03-30 00:11  findumars  Views(1168)  Comments(0Edit  收藏  举报