Qt学习笔记,再次分析EVA源码之后得出的结论-QListView,QListViewItem(Qt3);Q3ListView,Q3ListViewItem(Qt4)
Qt学习笔记,再次分析EVA源码之后得出的结论-QListView,QListViewItem(Qt3);Q3ListView,Q3ListViewItem(Qt4)
今天再次分析了Eva的源码,也看了qt3中QListView和QListViewItem手册,在Eva中实现item paint的方式如下:
void EvaListViewItem::paintCell( QPainter * painter, const QColorGroup & colourGroup, int column, int width, int align ) { if( ! isVisible() ) return; if ( column == 0){ QBrush *brush; QPalette palette; EvaListView *lv = dynamic_cast<EvaListView *> (listView()); if( !lv ) return; palette = lv->viewport()->palette(); brush = 0; const BackgroundMode bgmode = lv->viewport()->backgroundMode(); const QColorGroup::ColorRole crole = QPalette::backgroundRoleFromMode( bgmode ); if ( colourGroup.brush( crole ) != lv->colorGroup().brush( crole ) ) painter->fillRect( 0, 0, width, height(), colourGroup.brush( crole ) ); else lv->paintEmptyArea( painter, QRect( 0, 0, width, height() ) ); if ( isSelected() ) { brush = new QBrush( palette.color(QPalette::Active, QColorGroup::Highlight) ); // painter->setPen( palette.color(QPalette::Active, QColorGroup::Text) ); } else { // painter->setPen( palette.color(QPalette::Normal, QColorGroup::Text) ); } int icon_width = 0; if(m_icon){ icon_width = lv->itemMargin() + m_icon->width(); } // TODO: Change the font for highlighted text m_richText->draw( painter, lv->itemMargin() + icon_width, 0, QRect( icon_width, 0, width, height() ), colourGroup, brush ); //setHeight( m_RichText->height() ); if(m_icon){ int xo = lv->itemMargin(); int yo = ( height() - m_icon->height() ) / 2; painter->drawPixmap( xo, yo, *m_icon ); } int height = m_richText->height(); if(m_icon && m_icon->height() > m_richText->height()){ height = m_icon->height(); } height += 4; setHeight( height ); widthChanged( 0 ); delete brush; } else { QListViewItem::paintCell( painter, colourGroup, column, width, align ); } }
注:上面的QListViewItem中qt4中应该改为Q3ListViewItem
再分析了Qt4中的QListWidget和QTreeWidget,得出的结论是:在qt4中qt放弃了对如Qt3中对Listview和ListViewItem的支持,在看Qt3的手册的时候可以看到:
void QListView::insertItem ( QListViewItem * i ) [virtual] Inserts item i into the list view as a top-level item. You do not need to call this unless you've called takeItem(i) or QListViewItem::takeItem(i) and need to reinsert i elsewhere.
有insertItem函数。再看看qt4中的某一个Q*Widget,比如就是QListWidget:
void QListWidget::insertItem ( int row, QListWidgetItem * item ) Inserts the item at the position in the list given by row. See also addItem().
可以看到他们就是一致的,不妨在看看QTreeWidget:
void QTreeWidget::insertTopLevelItem ( int index, QTreeWidgetItem * item ) Inserts the item at index in the top level in the view. If the item has already been inserted somewhere else it wont be inserted.
也有类似的操作。
我们知道在qt4中要像view中加入数据,就要使用qt4特有的interview框架的model/view机制。但是在qt3中向view中加入数据(item)就有直接的insert函数,在qt4中的Q*Widget也有类似的insert函数,所以:
在qt4中把qt3中类似的view的insertItem函数和class演变成了Qt4中的Q*Widget,当然他们的实现功能几乎没有太大不一样。