Displaying QTextEdit content into QTableView cell



  • Hi,

    I have a QTableView associated to QItemDelegate. I reimplemented the createEditor and paint methods :

    createEditor() method :
    @
    QWidget *ModelDelegate::createEditor(QWidget parent,
    const QStyleOptionViewItem &/
    option */,
    const QModelIndex & index) const
    {
    if (index.column() != 0)
    {
    QTextEdit * editor = new QTextEdit(parent);
    editor->setFixedHeight(150);
    return editor;
    }
    else
    return NULL; // First column is not editable
    }
    @

    paint() method :
    @
    void ModelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    QStyleOptionViewItemV4 optionV4 = option;
    initStyleOption(&optionV4, index);

     QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style();
    
     QTextDocument doc;
     QString value = index.model()->data(index, Qt::DisplayRole).toString();
     doc.setHtml(value);
    
     // Painting item without text
     optionV4.text = QString();
     style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);
    
     QAbstractTextDocumentLayout::PaintContext ctx;
    
     // Highlighting text if item is selected
    if (optionV4.state & QStyle::State_Selected)
         ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));
    else
        ctx.palette.setColor(QPalette::Text, QColor("white"));
    
     QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
     painter->save();
     painter->translate(textRect.topLeft());
     painter->setClipRect(textRect.translated(-textRect.topLeft()));
     doc.documentLayout()->draw(painter, ctx);
     painter->restore();
    

    }
    @
    Now, the problem is that when i edit a cell and insert formatted text with some images, those images are not correctly shown into the cell (instead i’ve a little blank icon).

    Any idea ?

    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.