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. Mouse cursor over QWidget

Mouse cursor over QWidget

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 38.4k 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.
  • B Offline
    B Offline
    bunjee
    wrote on last edited by
    #1

    Greeting Qt community,

    I'm looking for a way to maintain a "cursor hovered" property.

    Even when the mouse is pressed or when a window overlaps my QWidget.

    Is there a way to check if the cursor is inside a visible part of my QWidget ?

    Thanks ♥.

    B.A.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      you can use setMouseTracking() to enable mouse tracking and implement mouseMoveEvent() to check mouse position.
      For example

      @
      void MyWidget::mouseMoveEvent(QMouseEvent *_event)
      {
      if (this->rect().contains(_event->pos())) {
      // Mouse over Widget
      }
      else {
      // Mouse out of Widget
      }
      }
      @

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jsprenkle
        wrote on last edited by
        #3

        There's already a css property for hovered. If you're just using it to change the appearance of the widget you can use that without doing any coding.

        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by
          #4

          Another way is to install an event filter and look for the enter and leave events, a trivial example:

          @
          #include <QtGui>
          #include <QtCore>

          class Widget : public QWidget {
          public:
          Widget(QWidget *parent=0) : QWidget(parent) {
          installEventFilter(this);

              QVBoxLayout *l=new QVBoxLayout(this);
              label=new QLabel("(Default)", this);
              l->addWidget(label);
          }
          

          protected:
          bool eventFilter(QObject *object, QEvent *event){
          if(object==this && (event->type()==QEvent::Enter || event->type()==QEvent::Leave))
          label->setText(event->type()==QEvent::Enter ? "Hovering" : "Not Hovering");
          }

          private:
          QLabel *label;
          };

          int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);
          Widget w;
          w.show();

          return a.exec&#40;&#41;;
          

          }
          @

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0
          • T Offline
            T Offline
            Timmmm
            wrote on last edited by
            #5

            [quote author="mcosta" date="1365013118"]Hi,

            you can use setMouseTracking() to enable mouse tracking and implement mouseMoveEvent() to check mouse position.
            @[/quote]

            This doesn't work because mouseMoveEvent() isn't called unless the cursor is inside the widget, so your if() condition is always true!

            You can theoretically use grabMouse() but I feel like that is a potentially bad way to do what should be fairly simple.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Timmmm
              wrote on last edited by
              #6

              -Here is some better code:- See below
              @
              class MyWidget : public QWidget
              {
              // ...
              protected:
              bool event(QEvent* e) override;
              // ...
              };

              bool MyWidget::event(QEvent* e)
              {
              if (e->type() == QEvent::Enter)
              qDebug() << "Enter"; // Or whatever
              if (e->type()==QEvent::Leave)
              qDebug() << "Leave";

              return QWidget::event(e); // Or whatever parent class you have.
              }@

              Actually screw that - there are already leaveEvent() and enterEvent() functions in QWidget!

              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