Important: Please read the Qt Code of Conduct -

QTableView Hover effect on complete row

  • Qt : 4.8

    I have a problem in multiline text in tableview and like to have hover row effect.
    When the cursor moves from one colum to another , then the multiline text is displayed as elided Text with "..."

    Following delegate is created to achieve the hover effect over table rows:
    TableView Code:
    ui->tableView->setItemDelegate(delegate); ui->tableView->setStyleSheet(QString("QTableView::item:hover{}"));

    Delegate code:
    void UI::CustomTableItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    if (!painter->isActive()) return QStyledItemDelegate::paint(painter, option, index);
    QStyleOptionViewItem viewOption(option);
    initStyleOption(&viewOption, index);

    if ((option.state & QStyle::State_MouseOver) == QStyle::State_MouseOver)
        const QWidget *widget = static_cast<const QStyleOptionViewItemV3 *>(&option)->widget;
        const QTableView *view = qobject_cast<const QTableView *>(widget);
        QColor color;
        QBrush brush(color);
        for (int i = 0; i < index.model()->columnCount(); ++i)
            QModelIndex idx = index.model()->index(index.row(), i);
            QStyleOptionViewItem opt(viewOption);
            initStyleOption(&opt, idx);
            int x = view->columnViewportPosition(idx.column());
            int y = view->rowViewportPosition(idx.row());
            int itemWidth = view->columnWidth(idx.column());
            int itemHeight = view->rowHeight(idx.row());
            opt.rect = QRect(x, y, itemWidth, itemHeight);
            painter->fillRect(opt.rect, brush);
            QStyledItemDelegate::paint(painter, opt, idx);
    QStyledItemDelegate::paint(painter, option, index);


    QStyledItemDelegate::paint(painter, opt, idx);
    Above line paints only the full text when hover off happens, while repainting it ignores the '\n' character.

    Please help me.....(Do I need to enable set wordwrap?)

Log in to reply