Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView Hover effect on complete row

QTableView Hover effect on complete row

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 1.5k 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.
  • I Offline
    I Offline
    inforathinam
    wrote on last edited by
    #1

    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->setSelectionBehavior(QAbstractItemView::SelectRows);
    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;
        color.setNamedColor("FF00FF");
        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);
    

    }

    Observation:
    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?)

    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 Extensions
    • Unsolved