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.
  • SavizS Offline
    SavizS Offline
    Saviz
    wrote on 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?

    JoeCFDJ Pl45m4P 2 Replies Last reply
    0
    • SavizS Saviz

      @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?

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on 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
      • SavizS Saviz

        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?

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #2

        @Saviz Qt5 or Qt6?

        SavizS 1 Reply Last reply
        1
        • SavizS Saviz

          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?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on 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

          SavizS 1 Reply Last reply
          2
          • JoeCFDJ JoeCFD

            @Saviz Qt5 or Qt6?

            SavizS Offline
            SavizS Offline
            Saviz
            wrote on last edited by
            #4

            @JoeCFD Qt version 6.2 LTS

            1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @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.

              SavizS Offline
              SavizS Offline
              Saviz
              wrote on 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?

              JoeCFDJ 1 Reply Last reply
              0
              • SavizS Saviz

                @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?

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on 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
                • SavizS Saviz has marked this topic as solved on

                • Login

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