Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Displaying QTextEdit content into QTableView cell
Qt 6.12 LTS is out! See what's new in the release blog

Displaying QTextEdit content into QTableView cell

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved