C++ (Qt)painter->drawText(option.rect, Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter, index.data().toString());
#ifndef MYDELEGATE_H#define MYDELEGATE_H#include <QStyledItemDelegate>#include <QPainter>class mydelegate : public QStyledItemDelegate{ Q_OBJECTpublic: explicit mydelegate(QObject *parent = 0);signals:public slots: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;};#endif // MYDELEGATE_H
#include "mydelegate.h"mydelegate::mydelegate(QObject *parent) : QStyledItemDelegate(parent){}void mydelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ Q_ASSERT(index.isValid()); painter->drawText(option.rect,Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter,index.data().toString()); QStyleOptionViewItemV4 opt = option; initStyleOption(&opt, index); const QWidget *widget = QStyledItemDelegatePrivate::widget(option); QStyle *style = widget ? widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); //QStyledItemDelegate::paint(painter,option,index);}
void mydelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ Q_ASSERT(index.isValid()); painter->drawText(option.rect,Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter,index.data().toString()); QStyleOptionViewItemV4 opt = option; initStyleOption(&opt, index); const QWidget *widget = QStyledItemDelegatePrivate::widget(option); QStyle *style = widget ? widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); //QStyledItemDelegate::paint(painter,option,index);}
const QWidget *widget = QStyledItemDelegatePrivate::widget(option);QStyle *style = widget ? widget->style() : QApplication::style();
QStyledItemDelegatePrivate::widget(option);
void mydelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ painter->drawText(option.rect,Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter,index.data().toString()); QStyledItemDelegate::paint(painter,option,index);}
void mydelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ painter->drawText(option.rect,Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter,index.data().toString());}
QStyledItemDelegate::sizeHint(option,index)
void mydelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ painter->save(); // в доке написано что нужно вызывать до рисовки QStyleOptionViewItemV4 opt = option; initStyleOption(&opt, index); painter->drawText(option.rect,Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter,index.data().toString()); QStyledItemDelegate::sizeHint(option,index); painter->restore(); // после завершения рисовки}
painter->save();
painter->restore();
initStyleOption(&opt, index);
void mydelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QPixmap icon(":/Icon/icon/kde_folder24.png"); painter->save(); QStyleOptionViewItemV4 opt = option; initStyleOption(&opt, index); painter->drawPixmap(option.rect,icon); painter->drawText(option.rect,Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter,index.data().toString()); QStyledItemDelegate::sizeHint(option,index); painter->restore();}
painter->drawPixmp(QRect(0,0,24,24),icon);painter->drawText(QRect(0,icon.height(), , ),Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter,index.data().toString());