0

I'm trying to create a floating, layout-less widget but it doesn't seem to work with a QVideoWidget as it's parent?

Working MRE:

from PySide6 import QtWidgets as qtw

app = qtw.QApplication()
w1 = qtw.QWidget()
w2 = qtw.QWidget(w1)
w2.setStyleSheet("background-color: red")
w1.show()
w2.show()
app.exec()

Not working MRE:

from PySide6 import QtMultimediaWidgets as qtmw
from PySide6 import QtWidgets as qtw

app = qtw.QApplication()
w1 = qtmw.QVideoWidget()
w2 = qtw.QWidget(w1)
w2.setStyleSheet("background-color: red")
w1.show()
w2.show()
app.exec()

The code is supposed to overlay a red QWidget over the QWidget/QVideoWidget but the red QWidget is shown only when the parent is a QWidget, and not when it's a QVideoWidget, why could that be?

Neat
  • 61
  • 1
  • 5
  • 1
    Not sure which of the solutions that work for qt5 will still work for qt6. Probably the best bet is to [use a graphics-view](https://stackoverflow.com/a/52602390/984421). – ekhumoro Sep 26 '22 at 12:35
  • @ekhumoro Could you still post the solution(s) that worked for qt5 please? I'll have a look at the `GraphicsView` way as well. – Neat Sep 26 '22 at 13:09
  • 1
    You can [search for them on SO](https://www.google.com/search?q=site:stackoverflow.com%20QVideoWidget%20overlay) (e.g. [this random example](https://stackoverflow.com/a/48299092/984421)). I don't have any specific examples myself - I just know they exist. My hunch is that many of the qt5 ones probably won't still work with qt6 (due to all the [changes to qt multimedia](https://doc.qt.io/qt-6/qtmultimedia-changes-qt6.html)). However, the graphics view framework has components explictly designed for the job, so it should work in both qt5 and qt6. – ekhumoro Sep 26 '22 at 13:44
  • @ekhumoro Oh, I will do just that then. I did implement this with `QGraphicsView` once before, along with an image viewer so I was able to switch between an image viewer or a video player, but dropped the idea because of a reason I don't quite remember anymore, which is why I asked for the non-`QGraphicsView` examples. Hopefully I don't rediscover the problem I faced before. – Neat Sep 26 '22 at 13:52
  • 1
    @Neat It seems that the behavior has changed in Qt6: QVideoWidget actually adds a private QVideoWindow (a private QWindow subclass) and adds it using [`createWindowContainer()`](https://doc.qt.io/qt-6/qwidget.html#createWindowContainer), which automatically puts the created widget *on top* of the stacked order and as an opaque box, meaning that there is no way to put anything *above* it. – musicamante Sep 26 '22 at 18:13
  • @musicamante guess I will have to do it the `QGraphicsView` way then. What I'm creating is a program to both view images and play videos (not simultaneously), would you recommend I add both the `QPixmap` and the `QGraphicsVideoItem` onto a single `QGraphicsView`, switching between the two items when requested, or use two `QGraphicsView`s in a `QStackedWidget` instead, switching between the two `QGraphicsView`s when requested? I'm thinking of going with the latter since I had problems (I can't remember what) trying to implement the former in the past. – Neat Sep 27 '22 at 01:06
  • 1
    @Neat I cannot give you a proper suggestion, since I don't know the requirement of your program. That said, having a common interface is certainly better: switching between a QGraphicsView and another widget could complicate things, while having a common graphics view interface could be actually useful, while it might require a more complex programming interface on your side at first. Fundamentally, both QGraphicsPixmapItem and QGraphicsVideoItem are very similar, so you could consider writing a common ancestor QGraphicsItem class that has both children, and eventually switch their visibility. – musicamante Sep 27 '22 at 03:34
  • 1
    @Neat also note that, in the rare case the underlying media framework doesn't support a specific image but still supports a more standard one, you can still open the image as a QPixmap, save its buffer as one of the formats compatible with the media framework, and use [`setSourceDevice(buffer, QUrl())`](https://doc.qt.io/qt-6/qmediaplayer.html#setSourceDevice). In this way you just have a single "video item". – musicamante Sep 27 '22 at 03:39

0 Answers0