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. What's the best way to attach a QWidget to another QWidget in Pyside2?
Forum Updated to NodeBB v4.3 + New Features

What's the best way to attach a QWidget to another QWidget in Pyside2?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 659 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.
  • L Offline
    L Offline
    lachdanan
    wrote on last edited by
    #1

    Hi,

    Basically I have a control whose qt handle is exposed which is a QWidget (source).

    I have my own QWidget that has transparency. I have a timer event (callback not related to Qt) that resets my widget's position and size to that of the source widget.

    Sometimes there is an issue in this timer/callback and also I would prefer a method where this is done automatically by Qt using some other means, such as parenting.

    I tried parenting my widget to the source widget but I immediately lost the transparency of my widget.

    Is there a way to do what I need? Basically my widget is acting like an overlay to the source widget with the ability to toggle it off (visible=False).

    Thanks a lot in advance.

    jsulmJ 1 Reply Last reply
    0
    • L lachdanan

      Hi,

      Basically I have a control whose qt handle is exposed which is a QWidget (source).

      I have my own QWidget that has transparency. I have a timer event (callback not related to Qt) that resets my widget's position and size to that of the source widget.

      Sometimes there is an issue in this timer/callback and also I would prefer a method where this is done automatically by Qt using some other means, such as parenting.

      I tried parenting my widget to the source widget but I immediately lost the transparency of my widget.

      Is there a way to do what I need? Basically my widget is acting like an overlay to the source widget with the ability to toggle it off (visible=False).

      Thanks a lot in advance.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @lachdanan said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

      that resets my widget's position and size to that of the source widget.

      Why don't you simply use layouts?
      https://doc.qt.io/qt-6/layout.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lachdanan
        wrote on last edited by
        #3

        Thanks a lot but this only works for widgets with a parent right?

        Because as I mentioned parenting removed my widget's transparency. I don't know why or how to fix that. If you have any solution to that, then I can definitely use layouts.

        jsulmJ 1 Reply Last reply
        0
        • L lachdanan

          Thanks a lot but this only works for widgets with a parent right?

          Because as I mentioned parenting removed my widget's transparency. I don't know why or how to fix that. If you have any solution to that, then I can definitely use layouts.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @lachdanan said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

          I don't know why or how to fix that

          It should work. Please show the code where you use layouts and set transparency.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lachdanan
            wrote on last edited by lachdanan
            #5

            @jsulm said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

            @lachdanan said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

            I don't know why or how to fix that

            It should work. Please show the code where you use layouts and set transparency.

            I didnt use layouts yet but I set opacity like this:
            mywidget.setWindowOpacity(0.5)
            mywidget.setWindowFlags(QtCore.Qt.Tool | QtCore.Qt.FramelessWindowHint)

            After I set its parent though, the transparency is gone.

            Someone here is saying opacity is not available for widgets with parent:
            https://stackoverflow.com/questions/67507113/setwindowopacity-not-working-for-a-child-widget

            B 1 Reply Last reply
            0
            • L lachdanan

              @jsulm said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

              @lachdanan said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

              I don't know why or how to fix that

              It should work. Please show the code where you use layouts and set transparency.

              I didnt use layouts yet but I set opacity like this:
              mywidget.setWindowOpacity(0.5)
              mywidget.setWindowFlags(QtCore.Qt.Tool | QtCore.Qt.FramelessWindowHint)

              After I set its parent though, the transparency is gone.

              Someone here is saying opacity is not available for widgets with parent:
              https://stackoverflow.com/questions/67507113/setwindowopacity-not-working-for-a-child-widget

              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              @lachdanan Yes, because it is setWindowOpacity, so this property only works for top-level windows.
              Child widgets have other ways to make them half transparent, like set colors with alpha channel, or use QGraphicsOpacityEffect.

              L 1 Reply Last reply
              0
              • B Bonnie

                @lachdanan Yes, because it is setWindowOpacity, so this property only works for top-level windows.
                Child widgets have other ways to make them half transparent, like set colors with alpha channel, or use QGraphicsOpacityEffect.

                L Offline
                L Offline
                lachdanan
                wrote on last edited by lachdanan
                #7

                @Bonnie said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

                @lachdanan Yes, because it is setWindowOpacity, so this property only works for top-level windows.
                Child widgets have other ways to make them half transparent, like set colors with alpha channel, or use QGraphicsOpacityEffect.

                Thanks what I am trying now but still I got no opacity yet:

                o=QtWidgets.QGraphicsOpacityEffect()
                o.setOpacity(0.2)
                mywidget.setGraphicsEffect(o)
                mywidget.setAutoFillBackground(True)

                Parenting works though but I need transparency.

                EDIT: I think the issue might be that the widget itself hosts an OpenGL window so maybe Opacity Effect doesn't work on that. setWindowOpacity works well, but like I mentioned then I can't do parenting.

                B 1 Reply Last reply
                0
                • L lachdanan

                  @Bonnie said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

                  @lachdanan Yes, because it is setWindowOpacity, so this property only works for top-level windows.
                  Child widgets have other ways to make them half transparent, like set colors with alpha channel, or use QGraphicsOpacityEffect.

                  Thanks what I am trying now but still I got no opacity yet:

                  o=QtWidgets.QGraphicsOpacityEffect()
                  o.setOpacity(0.2)
                  mywidget.setGraphicsEffect(o)
                  mywidget.setAutoFillBackground(True)

                  Parenting works though but I need transparency.

                  EDIT: I think the issue might be that the widget itself hosts an OpenGL window so maybe Opacity Effect doesn't work on that. setWindowOpacity works well, but like I mentioned then I can't do parenting.

                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by
                  #8

                  @lachdanan That's right, QGraphicsOpacityEffect can't work with open-gl based window.
                  What contents do your half-transparent widget have? If it also have some child widgets then it is not easy.

                  L 1 Reply Last reply
                  0
                  • B Bonnie

                    @lachdanan That's right, QGraphicsOpacityEffect can't work with open-gl based window.
                    What contents do your half-transparent widget have? If it also have some child widgets then it is not easy.

                    L Offline
                    L Offline
                    lachdanan
                    wrote on last edited by
                    #9

                    @Bonnie said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

                    @lachdanan That's right, QGraphicsOpacityEffect can't work with open-gl based window.
                    What contents do your half-transparent widget have? If it also have some child widgets then it is not easy.

                    It doesnt have any more child widgets. It just shows a node graph:
                    5c4dd596-22b3-476c-aab1-301b5206afb7-image.png

                    Because it's all contained in a window, setWindowOpacity works, and I don't have to do anything except make sure to have the same position and size as the source widget.

                    B 1 Reply Last reply
                    0
                    • L lachdanan

                      @Bonnie said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

                      @lachdanan That's right, QGraphicsOpacityEffect can't work with open-gl based window.
                      What contents do your half-transparent widget have? If it also have some child widgets then it is not easy.

                      It doesnt have any more child widgets. It just shows a node graph:
                      5c4dd596-22b3-476c-aab1-301b5206afb7-image.png

                      Because it's all contained in a window, setWindowOpacity works, and I don't have to do anything except make sure to have the same position and size as the source widget.

                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by Bonnie
                      #10

                      @lachdanan Still not sure what is it. Is that a QLabel with a picture?
                      Anyway, if the widget is very simple, just making the contents themself half-transparent, it is possible for something like pictures , foregound color, background color, or custom painting (by setting painter's opacity), etc.
                      If that is not applicable, then I would choose to stick with your two-window solution.
                      But rather than using a timer, I would prefer handling moveEvent and resizeEvent.

                      L 1 Reply Last reply
                      0
                      • B Bonnie

                        @lachdanan Still not sure what is it. Is that a QLabel with a picture?
                        Anyway, if the widget is very simple, just making the contents themself half-transparent, it is possible for something like pictures , foregound color, background color, or custom painting (by setting painter's opacity), etc.
                        If that is not applicable, then I would choose to stick with your two-window solution.
                        But rather than using a timer, I would prefer handling moveEvent and resizeEvent.

                        L Offline
                        L Offline
                        lachdanan
                        wrote on last edited by
                        #11

                        @Bonnie said in What's the best way to attach a QWidget to another QWidget in Pyside2?:

                        @lachdanan Still not sure what is it. Is that a QLabel with a picture?
                        Anyway, if the widget is very simple, just making the contents themself half-transparent, it is possible for something like pictures , foregound color, background color, or custom painting, etc.
                        If that is not applicable, then I would choose to stick with your two-window solution.
                        But rather than using a timer, I would prefer handling moveEvent and resizeEvent.

                        It's not. It's the OpenGL window which is in the host app. I don't have any control over this other than getting a qt handle, but the internals is not even qt.

                        You mean the move and resize event of the source widget? I would have to install event filters to the source widget(s).

                        Then I guess all of these are more complicated than my current solution.

                        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