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 hide/unhide QWidget on hover.
QtWS25 Last Chance

How to hide/unhide QWidget on hover.

Scheduled Pinned Locked Moved Solved General and Desktop
qvideowidgetqwidgethoverhideunhide
6 Posts 3 Posters 1.2k 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
    Saviz
    wrote on 26 Feb 2023, 02:41 last edited by
    #1

    I am trying to create a media player.

    I have a QVideoWidget that contains a QWidget, as a child inside of it, at the bottom.

    This is how it currently looks:

    fc11f75b-a653-4ef0-a646-e740ab43ee11-image.png

    It is fine, but I want to make sure that the QWidget at the bottom containing the tools will only show up if the user actually hovers over the video (in this case being the QVideoWidget)

    How can I achieve this?

    J P 2 Replies Last reply 26 Feb 2023, 04:00
    0
    • S Saviz
      26 Feb 2023, 04:49

      @Pl45m4 Thank you for your answer. Just one more question:

      If I am not mistaking in order to re-implement the events of the QVideoWidget I would have to make a subclasses of it and then insert virtual functions and overwrite them. Correct? But I currently have a QVideoWidget set in the designer ui form using the promote to method. How can I achieve this without all that work?

      J Offline
      J Offline
      JoeCFD
      wrote on 26 Feb 2023, 04:54 last edited by JoeCFD
      #6

      @Saviz

      #include <QApplication>
      #include <QMouseEvent>
      #include <QVideoWidget>
      
      class HoverVideoWidget : public QVideoWidget
      {
      public:
          HoverVideoWidget(QWidget *parent = nullptr) : QVideoWidget(parent)
          {
              setMouseTracking(true); // Enable mouse tracking to get hover events
          }
      
      protected:
          void enterEvent(QEvent *event) override
          {
              // Called when the mouse enters the widget
              setStyleSheet("background-color: lightblue;"); // Set the background color to light blue
          }
      
          void leaveEvent(QEvent *event) override
          {
              // Called when the mouse leaves the widget
              setStyleSheet(""); // Reset the background color to default
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          HoverVideoWidget player;
          player.show();
          return app.exec();
      }
      
      1 Reply Last reply
      1
      • S Saviz
        26 Feb 2023, 02:41

        I am trying to create a media player.

        I have a QVideoWidget that contains a QWidget, as a child inside of it, at the bottom.

        This is how it currently looks:

        fc11f75b-a653-4ef0-a646-e740ab43ee11-image.png

        It is fine, but I want to make sure that the QWidget at the bottom containing the tools will only show up if the user actually hovers over the video (in this case being the QVideoWidget)

        How can I achieve this?

        J Offline
        J Offline
        JoeCFD
        wrote on 26 Feb 2023, 04:00 last edited by
        #2

        @Saviz Qt5 or Qt6?

        S 1 Reply Last reply 26 Feb 2023, 04:41
        1
        • S Saviz
          26 Feb 2023, 02:41

          I am trying to create a media player.

          I have a QVideoWidget that contains a QWidget, as a child inside of it, at the bottom.

          This is how it currently looks:

          fc11f75b-a653-4ef0-a646-e740ab43ee11-image.png

          It is fine, but I want to make sure that the QWidget at the bottom containing the tools will only show up if the user actually hovers over the video (in this case being the QVideoWidget)

          How can I achieve this?

          P Offline
          P Offline
          Pl45m4
          wrote on 26 Feb 2023, 04:14 last edited by Pl45m4
          #3

          @Saviz

          Enable mouseTracking on your QVideoWidget with

          videoWidget->setMouseTracking(true);
          

          Re-implement enterEvent(QEnterEvent *) and leaveEvent(QEvent *) and call a function to hide/show the child widget with your controls (QHoverEvent might also work. Maybe even better).
          To make it look better, you could use something like QPropertyAnimation to let the widget fade-in and fade-out again, as soon as you leave the area.

          You could also install an eventFilter on your main widget, where you filter events coming from your video widget. Check, if event types are QEvent::Enter or QEvent::Leave and then handle it as needed.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          S 1 Reply Last reply 26 Feb 2023, 04:49
          2
          • J JoeCFD
            26 Feb 2023, 04:00

            @Saviz Qt5 or Qt6?

            S Offline
            S Offline
            Saviz
            wrote on 26 Feb 2023, 04:41 last edited by
            #4

            @JoeCFD Qt version 6.2 LTS

            1 Reply Last reply
            0
            • P Pl45m4
              26 Feb 2023, 04:14

              @Saviz

              Enable mouseTracking on your QVideoWidget with

              videoWidget->setMouseTracking(true);
              

              Re-implement enterEvent(QEnterEvent *) and leaveEvent(QEvent *) and call a function to hide/show the child widget with your controls (QHoverEvent might also work. Maybe even better).
              To make it look better, you could use something like QPropertyAnimation to let the widget fade-in and fade-out again, as soon as you leave the area.

              You could also install an eventFilter on your main widget, where you filter events coming from your video widget. Check, if event types are QEvent::Enter or QEvent::Leave and then handle it as needed.

              S Offline
              S Offline
              Saviz
              wrote on 26 Feb 2023, 04:49 last edited by
              #5

              @Pl45m4 Thank you for your answer. Just one more question:

              If I am not mistaking in order to re-implement the events of the QVideoWidget I would have to make a subclasses of it and then insert virtual functions and overwrite them. Correct? But I currently have a QVideoWidget set in the designer ui form using the promote to method. How can I achieve this without all that work?

              J 1 Reply Last reply 26 Feb 2023, 04:54
              0
              • S Saviz
                26 Feb 2023, 04:49

                @Pl45m4 Thank you for your answer. Just one more question:

                If I am not mistaking in order to re-implement the events of the QVideoWidget I would have to make a subclasses of it and then insert virtual functions and overwrite them. Correct? But I currently have a QVideoWidget set in the designer ui form using the promote to method. How can I achieve this without all that work?

                J Offline
                J Offline
                JoeCFD
                wrote on 26 Feb 2023, 04:54 last edited by JoeCFD
                #6

                @Saviz

                #include <QApplication>
                #include <QMouseEvent>
                #include <QVideoWidget>
                
                class HoverVideoWidget : public QVideoWidget
                {
                public:
                    HoverVideoWidget(QWidget *parent = nullptr) : QVideoWidget(parent)
                    {
                        setMouseTracking(true); // Enable mouse tracking to get hover events
                    }
                
                protected:
                    void enterEvent(QEvent *event) override
                    {
                        // Called when the mouse enters the widget
                        setStyleSheet("background-color: lightblue;"); // Set the background color to light blue
                    }
                
                    void leaveEvent(QEvent *event) override
                    {
                        // Called when the mouse leaves the widget
                        setStyleSheet(""); // Reset the background color to default
                    }
                };
                
                int main(int argc, char *argv[])
                {
                    QApplication app(argc, argv);
                    HoverVideoWidget player;
                    player.show();
                    return app.exec();
                }
                
                1 Reply Last reply
                1
                • S Saviz has marked this topic as solved on 26 Feb 2023, 07:34

                3/6

                26 Feb 2023, 04:14

                • Login

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