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. How to avoid change in row color on mouse hover ?
Forum Updated to NodeBB v4.3 + New Features

How to avoid change in row color on mouse hover ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 633 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.
  • T Offline
    T Offline
    tushu
    wrote on last edited by tushu
    #1

    I have QTreeWidgetItem with different color. Whenever I hover with mouse, lines background color becomes white.
    But I do not want that. Whenever I will hover, color of the line should not change.
    Currently I have used qstyle sheet to enable hovering.

    myTree->setStyleSheet(GetTreeStyleSheet());  
     
    GetTreeStyleSheet()
    {
       QString style =
       "QTreeView::item:hover {"
          "border: 1px solid;"
          "height: 30px;"
         "color: #000000;"     
        "}";
    return style
    }
    

    I tried to tackle this issue with creating EventFilter and capturing Hover event.
    In hover event, I am trying to know, previous color. And trying to set that same color on mouse hover. But it is not working

    bool eventFilter(QObject *watched, QEvent *event)
    {
        Q_UNUSED(watched);
        bool filterEvent = false;
        switch (event->type()) {
        case QEvent::HoverMove:{
            QHoverEvent* hoverEvent = static_cast<QHoverEvent*>(event);
            QTreeWidgetItem *selectedItem = myTree->itemAt(hoverEvent->pos());
            if(selectedItem){
            QBrush b = selectedItem->background(0);
            QColor c = b.color();
           // selectedItem->setBackground(0,b);  // it does not impact any difference
            selectedItem->setForeground(0,b);  // because of this, text is not visible
            }
            break;
        }
    default:
            break;
        }
    
        return filterEvent;
    }
    

    How to avoid change in row color on mouse hover ?

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      Based on the code shown, it can't be told if an event filter is the most efficient way to achieve the goal, and if it works at all. Things could maybe just be solved by assigning the right style sheets to the right objects. So let's assume an event filter is the best solution: It should filter hover events over items of myTree, i.e. prevent them from getting delivered to the watched object.

      bool eventFilter(QObject *watched, QEvent *event)
      {
          Q_UNUSED(watched);
          if (event->type == QEvent::HoverMove) {
              QHoverEvent *hoverEvent = static_cast<QHoverEvent *>(event);
              if (myTree->itemAt(hoverEvent->pos())
                  return true;
          }
          return false;
      }
      

      Remarks to the existing code:

      1. It seems to assume that the event filter is reached after selectedItemhas seen it. It aims at correcting what has been done before. It's the other way round: The event filter is reached before the event gets delivered to selectedItem. (filter, opposite of exhaust pipe ;-)
      2. bool filterEvent = false; defines a variable that is never changed and returned at the end. So eventFilter always returns false and the event gets delivered to the object watched. Returning truesays "this event has been processed already and does not have to be delivered anywhere else."

      Software Engineer
      The Qt Company, Oslo

      T 1 Reply Last reply
      1
      • Axel SpoerlA Axel Spoerl

        Based on the code shown, it can't be told if an event filter is the most efficient way to achieve the goal, and if it works at all. Things could maybe just be solved by assigning the right style sheets to the right objects. So let's assume an event filter is the best solution: It should filter hover events over items of myTree, i.e. prevent them from getting delivered to the watched object.

        bool eventFilter(QObject *watched, QEvent *event)
        {
            Q_UNUSED(watched);
            if (event->type == QEvent::HoverMove) {
                QHoverEvent *hoverEvent = static_cast<QHoverEvent *>(event);
                if (myTree->itemAt(hoverEvent->pos())
                    return true;
            }
            return false;
        }
        

        Remarks to the existing code:

        1. It seems to assume that the event filter is reached after selectedItemhas seen it. It aims at correcting what has been done before. It's the other way round: The event filter is reached before the event gets delivered to selectedItem. (filter, opposite of exhaust pipe ;-)
        2. bool filterEvent = false; defines a variable that is never changed and returned at the end. So eventFilter always returns false and the event gets delivered to the object watched. Returning truesays "this event has been processed already and does not have to be delivered anywhere else."
        T Offline
        T Offline
        tushu
        wrote on last edited by
        #3

        @Axel-Spoerl
        Thanks for your reply.
        I wanted to solve this issue with qstyle sheet only. But I could not do that.
        So I tried different way.
        If it is possible, can you try with qStyleSheet ?

        Christian EhrlicherC 1 Reply Last reply
        0
        • T tushu

          @Axel-Spoerl
          Thanks for your reply.
          I wanted to solve this issue with qstyle sheet only. But I could not do that.
          So I tried different way.
          If it is possible, can you try with qStyleSheet ?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @tushu said in How to avoid change in row color on mouse hover ?:

          If it is possible, can you try with qStyleSheet ?

          This will not work (as you already found out)

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

          T 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @tushu said in How to avoid change in row color on mouse hover ?:

            If it is possible, can you try with qStyleSheet ?

            This will not work (as you already found out)

            T Offline
            T Offline
            tushu
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            I tried but I could not solve the issue with style sheet.

            "QTreeWidget::item:hover {"
                                  "background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 transparent, stop: 1 transparent);"
                                  "border: 1px solid #bfcde4;"
                      "}"
            

            And my 1st preference is Qstylesheet. But I stucked and I tried different way.
            Can anyone help me ?

            1 Reply Last reply
            0
            • JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              what is your qt version? Mine is 5.15.2 on ubuntu and I do not have this issue.

              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