What's the best way to attach a QWidget to another QWidget in Pyside2?
-
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.
-
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.
@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 -
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.
@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.
-
@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 -
@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@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 useQGraphicsOpacityEffect
. -
@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 useQGraphicsOpacityEffect
.@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 useQGraphicsOpacityEffect
.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.
-
@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 useQGraphicsOpacityEffect
.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.
@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. -
@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.@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:
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.
-
@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:
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.
@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 handlingmoveEvent
andresizeEvent
. -
@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 handlingmoveEvent
andresizeEvent
.@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 handlingmoveEvent
andresizeEvent
.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.