Creating QTableView TextEditDelegate

From QT Wikipedia

Jump to: navigation, search

Recently working with QTextEdit had little trouble because data stored in QTextEdit is usualy stored in ritch html format and QTableView shows text directly in HTML format with all unnecessary tags. I wanned that QTableView only showed plain text so implemented my Own Item delegate witch done conversion and suplied plain text to QTableView Paint command.

texteditdelegate.h

 
#ifndef TEXTEDITDELEGATE_H
#define TEXTEDITDELEGATE_H
 
#include <QItemDelegate>
 
class TextEditDelegate : public QItemDelegate
 {
     Q_OBJECT
 
 public:
     TextEditDelegate(QObject *parent = 0) : QItemDelegate(parent) {};
     void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const;     
 };
 
#endif
 

I'm sure that code needs some little optimization, but for now it does it work.

texteditdelegate.cpp

 
#include "texteditdelegate.h"
 
#include <QTextDocument>
#include <QModelIndex>
#include <QPainter>
 
void TextEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
{
 
   QTextDocument document;
   document.setHtml(index.model()->data(index, Qt::DisplayRole).toString());
   QString stringplain = document.toPlainText();
 
    Q_ASSERT(index.isValid());
 
    QStyleOptionViewItemV3 opt = setOptions(index, option);
 
    const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option);
    opt.features = v2 ? v2->features
                    : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None);
    const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option);
    opt.locale = v3 ? v3->locale : QLocale();
    opt.widget = v3 ? v3->widget : 0;
 
    // prepare
    painter->save();
 
    painter->setClipRect(opt.rect);
 
    // get the data and the rectangles
    QVariant value;
 
    QPixmap pixmap;
    QRect decorationRect;
    value = index.data(Qt::DecorationRole);
 
    QString text;
    QRect displayRect;
    value = index.data(Qt::DisplayRole);
    if (value.isValid()) {
        text = stringplain;
        displayRect = textRectangle(painter, option.rect, opt.font, text);
    }
 
    QRect checkRect;
    Qt::CheckState checkState = Qt::Unchecked;
    value = index.data(Qt::CheckStateRole);
    if (value.isValid()) {
        checkState = static_cast<Qt::CheckState>(value.toInt());
        checkRect = check(opt, opt.rect, value);
    }
 
    // do the layout
    doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
    // draw the item
 
    drawBackground(painter, opt, index);
    drawCheck(painter, opt, checkRect, checkState);
    drawDecoration(painter, opt, decorationRect, pixmap);
    drawDisplay(painter, opt, displayRect, text);
    drawFocus(painter, opt, displayRect);
 
    // done
    painter->restore();
}
 

Usage example

 
//...
    ui.ClientTableView->setItemDelegateForColumn(2,new TextEditDelegate);
    ui.ClientTableView->setItemDelegateForColumn(4,new TextEditDelegate);
//...
 

So now in column to witch data comes from QTextEdit i see plain text and it is what i waned.


Personal tools
Toolbox