Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to avoid change in row color on mouse hover ?

    General and Desktop
    4
    6
    183
    Loading More Posts
    • 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
      tushu last edited by tushu

      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 Reply Quote 0
      • Axel Spoerl
        Axel Spoerl last edited by

        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 Reply Quote 1
        • T
          tushu @Axel Spoerl last edited by

          @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 Ehrlicher 1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion @tushu last edited by

            @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 Reply Quote 0
            • T
              tushu @Christian Ehrlicher last edited by

              @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 Reply Quote 0
              • JoeCFD
                JoeCFD last edited by JoeCFD

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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post