python qt, display text/label above another widget

2019-02-11 08:12发布

问题:

I'm making a video player using PySide which is a python bind to the Qt framework. I'm using phonon(a module) to display the video and I want to display text above the video as a subtitle. How can I put another widget above my phonon widget. Is opengl an option?

回答1:

If you just create your label and set the phonon widget as the parent, the label should appear over it.

QLabel *label = new QLabel(phononWidget);
label->setText("Text over video!");

(I realize this is C++ and you are working in Python but it should be similar)

Update: The above will not work for hardware accelerated video playback. An alternative that does work is to create a graphics scene and add the video widget or player to the scene and use a QGraphicsTextItem for the text. Setting the viewport to a QGLWidget will enable hardware acceleration:

QGraphicsScene *scene = new QGraphicsScene(this);

Phonon::VideoPlayer *v = new Phonon::VideoPlayer();
v->load(Phonon::MediaSource("video_file"));

QGraphicsProxyWidget *pvideoWidget = scene->addWidget(v);

QGraphicsView *view = new QGraphicsView(scene);
view->setViewport(new QGLWidget); //Enable hardware acceleration!

QGraphicsTextItem *label = new QGraphicsTextItem("Text Over Video!", pvideoWidget);
label->moveBy(100, 100);

v->play();