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. Calling the hide function for a widget only hides the visual representation, not the widget itself
QtWS25 Last Chance

Calling the hide function for a widget only hides the visual representation, not the widget itself

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 560 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.
  • Q Offline
    Q Offline
    Quiccz
    wrote on last edited by
    #1

    Base on https://forum.qt.io/topic/151309/why-inherit-qwidget-cant-set-background-color-if-use-q_object-in-qt6-5?_=1698026074316

    All of my widgets inherit from QWidget. When a widget that uses winId() to display video calls hide(), the video display is hidden, but the widget's background remains visible. This indicates that the widget isn't hidden immediately. For another widget that also uses winId() (to test if it's due to the native window), but draws images in the PaintEvent, calling hide() results in no response, which is the expected behavior.
    c02cbdff-92a8-4d13-b29c-b8a14944511f-image.png
    2de11de3-7bdd-48bd-a013-64f0d99d288a-image.png
    ce992f3e-d082-45cb-8359-d5722fc0e474-image.png
    41162de2-82fc-417e-b60f-dcd915ecd34e-image.png
    Is there a way to make the hide action for the video widget behave the same way as the image widget's hide action?

    Christian EhrlicherC 1 Reply Last reply
    0
    • Q Quiccz

      Base on https://forum.qt.io/topic/151309/why-inherit-qwidget-cant-set-background-color-if-use-q_object-in-qt6-5?_=1698026074316

      All of my widgets inherit from QWidget. When a widget that uses winId() to display video calls hide(), the video display is hidden, but the widget's background remains visible. This indicates that the widget isn't hidden immediately. For another widget that also uses winId() (to test if it's due to the native window), but draws images in the PaintEvent, calling hide() results in no response, which is the expected behavior.
      c02cbdff-92a8-4d13-b29c-b8a14944511f-image.png
      2de11de3-7bdd-48bd-a013-64f0d99d288a-image.png
      ce992f3e-d082-45cb-8359-d5722fc0e474-image.png
      41162de2-82fc-417e-b60f-dcd915ecd34e-image.png
      Is there a way to make the hide action for the video widget behave the same way as the image widget's hide action?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please provide some code on how you show your widget - a minimal, compilable example.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Q 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Please provide some code on how you show your widget - a minimal, compilable example.

        Q Offline
        Q Offline
        Quiccz
        wrote on last edited by
        #3

        @Christian-Ehrlicher

        #pragma once
        #include <QWidget>
        #include <mpv/client.h>
        class InheritWidget : public QWidget
        {
        Q_OBJECT
        public:
           InheritWidget(QWidget* parent);
           void Init();
        private:
           mpv_handle* mpv;
        };
        
        #include "InheritWidget.h"
        
        InheritWidget::InheritWidget(QWidget* parent)
        	:QWidget(parent)
        {
        
        }
        
        void InheritWidget::Init()
        {
            mpv = mpv_create();
            if (!mpv)
            {
                qDebug() << "could not create mpv context";
                throw std::runtime_error("could not create mpv context");
            }
            int64_t  wid = this->winId();
            mpv_set_property(mpv, "wid", MPV_FORMAT_INT64, &wid);
        
            qDebug() << "wid is " << wid;
            mpv_set_property_string(mpv, "hwdec", "auto");
            mpv_set_property_string(mpv, "loop-file", "inf");
            if (mpv_initialize(mpv) < 0)
            {
                qDebug() << "could not initialize mpv context";
                throw std::runtime_error("could not initialize mpv context");
            }
            const char* args[] = { "loadfile", "4k_60.mp4", NULL };
            mpv_command_async(mpv, 0, args);
        }
        
        #pragma once
        #include <QWidget>
        #include "InheritWidget.h"
        class MyWidget: public QWidget
        {
        public:
            MyWidget(QWidget *parent);
            void SetSubWidget();
        
            void hide();
        private:
            InheritWidget* videoWidget;
        };
        
        #include "Mywidget.h"
        
        
        MyWidget::MyWidget(QWidget* parent)
        : QWidget(parent)
        {
            this->setGeometry(0,0, 1920, 1080);
            this->setWindowFlags(Qt::FramelessWindowHint);
            QPalette palette;
            palette.setColor(QPalette::Window, Qt::GlobalColor::white);
            this->setAutoFillBackground(true);
            this->setPalette(palette);
        }
        
        void MyWidget::SetSubWidget()
        {
            InheritWidget*subWidget = new InheritWidget(this);
            subWidget->setGeometry(0, 0, 1000, 1000);
            QPalette palette;
            palette.setColor(QPalette::Window, Qt::GlobalColor::black);
            subWidget->setAutoFillBackground(true);
            subWidget->winId();
            subWidget->setPalette(palette);
            subWidget->show();
        
            InheritWidget* ssubWidget = new InheritWidget(subWidget);
            ssubWidget->setGeometry(0, 0, 960, 540);
            QPalette palette1;
            palette1.setColor(QPalette::Window, Qt::GlobalColor::red);
            ssubWidget->setAutoFillBackground(true);
            ssubWidget->winId();
            ssubWidget->setPalette(palette1);
            ssubWidget->Init();
            ssubWidget->show();
            videoWidget = ssubWidget;
        }
        
        void MyWidget::hide()
        {
            videoWidget->hide();
        }
        
        #include <QWidget>
        #include "Mywidget.h"
        #include <QApplication>
        #include <QTimer>
        
        
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
            app.setAttribute(Qt::AA_NativeWindows);
            MyWidget *mywidget = new MyWidget(nullptr);
            mywidget->show();
            QTimer::singleShot(2000, mywidget, [&]() {
                mywidget->SetSubWidget();
             });
            QTimer::singleShot(5000, mywidget, [&]() {
                mywidget->hide();
                });
            
            return app.exec();;
        }
        

        Do you need videofile and libmpv?

        C 1 Reply Last reply
        0
        • Q Quiccz

          @Christian-Ehrlicher

          #pragma once
          #include <QWidget>
          #include <mpv/client.h>
          class InheritWidget : public QWidget
          {
          Q_OBJECT
          public:
             InheritWidget(QWidget* parent);
             void Init();
          private:
             mpv_handle* mpv;
          };
          
          #include "InheritWidget.h"
          
          InheritWidget::InheritWidget(QWidget* parent)
          	:QWidget(parent)
          {
          
          }
          
          void InheritWidget::Init()
          {
              mpv = mpv_create();
              if (!mpv)
              {
                  qDebug() << "could not create mpv context";
                  throw std::runtime_error("could not create mpv context");
              }
              int64_t  wid = this->winId();
              mpv_set_property(mpv, "wid", MPV_FORMAT_INT64, &wid);
          
              qDebug() << "wid is " << wid;
              mpv_set_property_string(mpv, "hwdec", "auto");
              mpv_set_property_string(mpv, "loop-file", "inf");
              if (mpv_initialize(mpv) < 0)
              {
                  qDebug() << "could not initialize mpv context";
                  throw std::runtime_error("could not initialize mpv context");
              }
              const char* args[] = { "loadfile", "4k_60.mp4", NULL };
              mpv_command_async(mpv, 0, args);
          }
          
          #pragma once
          #include <QWidget>
          #include "InheritWidget.h"
          class MyWidget: public QWidget
          {
          public:
              MyWidget(QWidget *parent);
              void SetSubWidget();
          
              void hide();
          private:
              InheritWidget* videoWidget;
          };
          
          #include "Mywidget.h"
          
          
          MyWidget::MyWidget(QWidget* parent)
          : QWidget(parent)
          {
              this->setGeometry(0,0, 1920, 1080);
              this->setWindowFlags(Qt::FramelessWindowHint);
              QPalette palette;
              palette.setColor(QPalette::Window, Qt::GlobalColor::white);
              this->setAutoFillBackground(true);
              this->setPalette(palette);
          }
          
          void MyWidget::SetSubWidget()
          {
              InheritWidget*subWidget = new InheritWidget(this);
              subWidget->setGeometry(0, 0, 1000, 1000);
              QPalette palette;
              palette.setColor(QPalette::Window, Qt::GlobalColor::black);
              subWidget->setAutoFillBackground(true);
              subWidget->winId();
              subWidget->setPalette(palette);
              subWidget->show();
          
              InheritWidget* ssubWidget = new InheritWidget(subWidget);
              ssubWidget->setGeometry(0, 0, 960, 540);
              QPalette palette1;
              palette1.setColor(QPalette::Window, Qt::GlobalColor::red);
              ssubWidget->setAutoFillBackground(true);
              ssubWidget->winId();
              ssubWidget->setPalette(palette1);
              ssubWidget->Init();
              ssubWidget->show();
              videoWidget = ssubWidget;
          }
          
          void MyWidget::hide()
          {
              videoWidget->hide();
          }
          
          #include <QWidget>
          #include "Mywidget.h"
          #include <QApplication>
          #include <QTimer>
          
          
          int main(int argc, char *argv[])
          {
              QApplication app(argc, argv);
              app.setAttribute(Qt::AA_NativeWindows);
              MyWidget *mywidget = new MyWidget(nullptr);
              mywidget->show();
              QTimer::singleShot(2000, mywidget, [&]() {
                  mywidget->SetSubWidget();
               });
              QTimer::singleShot(5000, mywidget, [&]() {
                  mywidget->hide();
                  });
              
              return app.exec();;
          }
          

          Do you need videofile and libmpv?

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @Quiccz I do not see the behaviour you describe with respect to the background of the "ssubWidget" on Ubuntu/KDE/Qt 6.6. The widget playing video is hidden and the MPV widget black background is all I see (the video is still playing and audio is there).

          It is, however, unclear what you are trying to achieve with the somewhat circuitous setup of nested widgets without layouts.

          Q 1 Reply Last reply
          0
          • C ChrisW67

            @Quiccz I do not see the behaviour you describe with respect to the background of the "ssubWidget" on Ubuntu/KDE/Qt 6.6. The widget playing video is hidden and the MPV widget black background is all I see (the video is still playing and audio is there).

            It is, however, unclear what you are trying to achieve with the somewhat circuitous setup of nested widgets without layouts.

            Q Offline
            Q Offline
            Quiccz
            wrote on last edited by Quiccz
            #5

            @ChrisW67 Did you use breakpoint in mpvwidget->hide(); i mean after call hide(), mpvwidget show backgroud color immediately

            I'm working on a player that receives a JSON playlist, and it has no border and the window is non-resizable. Therefore, I'm using a layered approach with multiple widgets to achieve this.

            like

            {
            	"program1": [
            		{
            			"x" : 0,
            			"y" : 0,
            			"width" : 100,
            			"height" : 100,
            			"file" : "1.mp4"
            		},
            		{
            			"x" : 100,
            			"y" : 100,
            			"width" : 100,
            			"height" : 100,
            			"file" : "2.mp4"
            		}
            	],
            	"program2": [
            		{
            			"x" : 0,
            			"y" : 0,
            			"width" : 100,
            			"height" : 100,
            			"file" : "3.jpg"
            		},
            		{
            			"x" : 100,
            			"y" : 100,
            			"width" : 100,
            			"height" : 100,
            			"file" : "4.jpg"
            		}
            	]
            }
            

            5886a4b8-bc65-4526-8504-924bd6d9425b-image.png

            C 1 Reply Last reply
            0
            • Q Quiccz

              @ChrisW67 Did you use breakpoint in mpvwidget->hide(); i mean after call hide(), mpvwidget show backgroud color immediately

              I'm working on a player that receives a JSON playlist, and it has no border and the window is non-resizable. Therefore, I'm using a layered approach with multiple widgets to achieve this.

              like

              {
              	"program1": [
              		{
              			"x" : 0,
              			"y" : 0,
              			"width" : 100,
              			"height" : 100,
              			"file" : "1.mp4"
              		},
              		{
              			"x" : 100,
              			"y" : 100,
              			"width" : 100,
              			"height" : 100,
              			"file" : "2.mp4"
              		}
              	],
              	"program2": [
              		{
              			"x" : 0,
              			"y" : 0,
              			"width" : 100,
              			"height" : 100,
              			"file" : "3.jpg"
              		},
              		{
              			"x" : 100,
              			"y" : 100,
              			"width" : 100,
              			"height" : 100,
              			"file" : "4.jpg"
              		}
              	]
              }
              

              5886a4b8-bc65-4526-8504-924bd6d9425b-image.png

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              @Quiccz said in Calling the hide function for a widget only hides the visual representation, not the widget itself:

              Did you use breakpoint in mpvwidget->hide(); i mean after call hide(), mpvwidget show backgroud color immediately

              Why would I do that? You certainly didn't say you were deliberately halting program execution. Halting execution after calling hide() but before the Qt event loop is reached more-or-less guarantees that part or all of the rendering is incomplete.

              Q 2 Replies Last reply
              3
              • C ChrisW67

                @Quiccz said in Calling the hide function for a widget only hides the visual representation, not the widget itself:

                Did you use breakpoint in mpvwidget->hide(); i mean after call hide(), mpvwidget show backgroud color immediately

                Why would I do that? You certainly didn't say you were deliberately halting program execution. Halting execution after calling hide() but before the Qt event loop is reached more-or-less guarantees that part or all of the rendering is incomplete.

                Q Offline
                Q Offline
                Quiccz
                wrote on last edited by
                #7

                @ChrisW67 Because when switching videos in my program, I noticed a brief moment where a red background was visible. So, I added breakpoints to debug. I discovered that when I used video hide(), the video frame disappeared immediately instead of staying on the last frame until the next event loop. I was just curious about why this was happening. Of course, I eventually resolved the issue by changing the order of hide() and show()

                1 Reply Last reply
                0
                • C ChrisW67

                  @Quiccz said in Calling the hide function for a widget only hides the visual representation, not the widget itself:

                  Did you use breakpoint in mpvwidget->hide(); i mean after call hide(), mpvwidget show backgroud color immediately

                  Why would I do that? You certainly didn't say you were deliberately halting program execution. Halting execution after calling hide() but before the Qt event loop is reached more-or-less guarantees that part or all of the rendering is incomplete.

                  Q Offline
                  Q Offline
                  Quiccz
                  wrote on last edited by Quiccz
                  #8

                  @ChrisW67 If you overlay a transparent widget on top of the MPVWidget, you will notice that you see the background color of the MVPWidget instead of the video. I believe this is for the same reason as the immediate display of the video's background when using hide().
                  51c72813-f8ed-4e51-9e60-b52b1ccf2d1a-image.png
                  158b4ce8-0287-4291-9c49-96cdb5ded55d-image.png

                  1 Reply Last reply
                  0
                  • Q Quiccz referenced this topic on

                  • Login

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