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. Highlight QtreeView row on mouse hover
Forum Updated to NodeBB v4.3 + New Features

Highlight QtreeView row on mouse hover

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 893 Views
  • 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.
  • S Offline
    S Offline
    seamoce
    wrote on last edited by
    #1

    Hi all,

    I have a cpp qwidget with a QTreeview and a QSqlTableModel inside. I would like to highlight on mouse hover an entire row. Curenntly the behaviour is made to highlight just one cell. Do you have any suggestion how to do this.

    What I tried:

    • Set the stylesheet
    ui->myTableView->setStyleSheet("::item:hover{background-color: rgb(249, 175, 42);}");
    
    • Sublcass the QSqlTableModel in order to overwrite the data method using the Qt::BackgroundRole
    // setData() method
    if (role == Qt::BackgroundRole)
    {
       QBrush redBackground(Qt::red);
       return redBackground;
    }
    

    and on the view I installed the event filter in order to catch the mouse hover event and then then call the setData() method. My problem is that this gets overwritten by the current stylesheet of the application, and when I disable the stylesheet then the setData set the brush for all the cells. How can I manage this?

    Thanks.
    Simone

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Use a custom QStyledItemDelegate and set QStyle::State_MouseOver in QStyleOptionViewItem.state for every index in your row or derive from QTableView,overwrite initViewItemOption() and set State_MouseOver there (only for Qt6).

      Note: some styles might ignore this flag.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2

      • Login

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