List view not Displaying all The Time
-
Hi ,
I have a problem with displaying List view. We are having an customized item delegate for this list where we overwrite the paint event.The Problem is the list does not display on UI When it is loaded.The list is displayed when we generate an mouse Press event in list.Please Give your Ideas and suggestion to solve this.
I am adding customized delegate code for your better understanding.
@void CItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
painter->save();if( option.state & QStyle::State_Sunken ) { painter->fillRect( option.rect, option.palette.highlight() ); } if( m_connectionStatus && ( m_selectedIndex == index.row() ) ) { QPixmap selectionIndicator = icons[10]; painter->drawPixmap( option.rect.width() - selectionIndicator.width() - 10, option.rect.top() + option.rect.height() / 2 - selectionIndicator.height() / 2, selectionIndicator ); } // Draw the icon QVariantList fields = index.data().toList(); QPixmap icon = icons[fields[IconIndex].toInt()]; painter->drawPixmap( 14, option.rect.top() + option.rect.height() / 2 - icon.height() / 2, icon ); QFont f( painter->font() ); f.setPixelSize( 23 ) ; f.setBold( true ); painter->setFont( f ); // Draw the error description text, truncated if necessary static const int TextLeftMargin = 80; static const int TextTopBottomMargin = 25; QString elidedDescription = painter->fontMetrics().elidedText( fields[Description].toString(), Qt::ElideRight, 352 ); painter->drawText( option.rect.adjusted( TextLeftMargin, TextTopBottomMargin, 0, 0 ), Qt::AlignLeft, elidedDescription ); // Divider QRect dividerRect = option.rect; dividerRect.setTop( dividerRect.bottom() ); QLinearGradient gradient( 0, 0, option.rect.width() / 2, 0 ); gradient.setColorAt( 0, QColor( 144, 144, 144, 0 ) ); gradient.setColorAt( 1, QColor( 144, 144, 144, 255 ) ); gradient.setSpread( QGradient::ReflectSpread ); painter->fillRect( dividerRect, gradient ); painter->restore();
}@
Thanks In Advance.
-
Hi,
Did you check whether the delegate's paint() is called when application loads ?
Did you reimplement sizeHint() ? -
Hi,
afaik if the item delegate is set before entering the app's event loop (first showing) then the overridden paint is called ...
-
Yes.I implemented the SizeHint() function
-
I observe that my listview paintevent is called on first time load,but the listview does not display anything, my code is below:
void CListView::paintEvent( QPaintEvent* e )
{QListView::paintEvent( e );//I get QPainter::begin: Widget painting can only begin as a result of a paintEvent
if( pressedIndex.isValid() )//I want to custom paint only when a row is pressed { qDebug( "CListView::paintEvent() - pressed Index valid" ); QPainter painter( viewport() ); QStyleOptionViewItem option = viewOptions(); option.rect = visualRect( pressedIndex ); if( !draggedAway ) { option.state |= QStyle::State_Sunken; } if( pressedIndex == currentIndex() ) { option.state |= QStyle::State_Selected; } itemDelegate()->paint( &painter, option, pressedIndex ); } else { qDebug( "CListView::paintEvent() - pressed Index Invalid" ); }
}
-
Hi,
You should rather handle that directly in the paint function of your delegate.