QStyleOptionViewItemV4 option; option.initFrom(this); option.state |= QStyle::State_HasFocus; option.state |= QStyle::State_MouseOver; option.palette.setCurrentColorGroup(QPalette::Normal); option.state |= QStyle::State_Selected; option.rect = QRect(20,10,200,200); option.direction = Qt::LeftToRight; style()->drawControl(QStyle::CE_ItemViewItem, &option, &painter, this);
void QMyLabel::paintEvent(QPaintEvent* e){ //QLabel::paintEvent(e); QPainter painter(this); QRect rect(QRect(QPoint(),size())); QStyleOptionViewItemV4 option; option.initFrom(this); option.state |= QStyle::State_HasFocus; option.state |= QStyle::State_MouseOver; option.palette.setCurrentColorGroup(QPalette::Normal); option.state |= QStyle::State_Selected; option.rect = QRect(20,10,200,200); option.direction = Qt::LeftToRight; style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, &painter, this);}
C++ (Qt) case PE_PanelItemViewItem: { const QStyleOptionViewItemV4 *vopt; const QAbstractItemView *view = qobject_cast<const QAbstractItemView *>(widget); bool newStyle = false; if (const QListView *listview = qobject_cast<const QListView *>(widget)) { if (listview->viewMode() == QListView::IconMode) newStyle = true; } else if (qobject_cast<const QTreeView *>(widget)) { newStyle = true; } if (newStyle && view && (vopt = qstyleoption_cast<const QStyleOptionViewItemV4 *>(option))) {//... } else { QWindowsXPStyle::drawPrimitive(element, option, painter, widget); } break; }