class Delegate: public QStyledItemDelegate { int aFlags;public: Delegate( int flags ) : aFlags( Qt::AlignVCenter | flags ) { } void paint( QPainter* p, const QStyleOptionViewItem& o, const QModelIndex& i ) const { Q_ASSERT( i.isValid() ); QStyleOptionViewItemV4 opt = o; initStyleOption( &opt, i ); opt.displayAlignment = (Qt::Alignment) aFlags; QStyle *style = QApplication::style(); style->drawControl( QStyle::CE_ItemViewItem, &opt, p, NULL ); }};
class place2text_delegate: public QItemDelegate {public: void paint( QPainter* p, const QStyleOptionViewItem& o, const QModelIndex& i ) const { Q_ASSERT( i.isValid() ); if( i.data().toInt() == 0 ){ QString text = QString(tr("В библиотеке")); drawDisplay(p, o, o.rect, text); } else if( i.data().toInt() == 1){ QString text = QString(tr("У студента")); drawDisplay(p, o, o.rect, text); } else{ QString text = QString(tr("Неверные данные в БД")); drawDisplay(p, o, o.rect, text); } drawFocus(p, o, o.rect); }};
ui->booksView->setItemDelegateForColumn( 6, new place2text_delegate() );