Qt Forum

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

    Mouse cursor over QWidget

    General and Desktop
    5
    6
    36874
    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.
    • B
      bunjee last edited by

      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 Reply Quote 0
      • M
        mcosta last edited by

        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 Reply Quote 1
        • J
          jsprenkle last edited by

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

            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 Reply Quote 0
            • T
              Timmmm last edited by

              [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 Reply Quote 0
              • T
                Timmmm last edited by

                -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 Reply Quote 0
                • First post
                  Last post