How to draw on the top of an inherited widget (VlcVideoWidget)?

ぃ、小莉子 提交于 2019-12-06 20:34:25

It looks like you want to create an overlay. If you take a look at WidgetVideo.cpp in the source for vlc-qt, you can see that the request() method creates a widget and adds it to a layout which is parented to the VlcVideoWidget. This is probably messing with the overlay you're painting in your paintEvent.

To create an overlay that should stay on top of your video, follow the method outlined here: http://developer.nokia.com/community/wiki/How_to_overlay_QWidget_on_top_of_another

You should add an instance of the overlay class to your instance of TrackerWidgetVideo. The overlay class will contain the overriden paintEvent method that is currently part of your TrackerWidgetVideo. Then, you'll override TrackerWidgetVideo::resizeEvent to resize your overlay class instance.

Here's some example code:

Overlay.h

class Overlay : public QWidget
{
    Q_OBJECT

 public:
    Overlay(QWidget* parent);

 protected:
    void paintEvent(QPaintEvent* event);
};

Overlay.cpp

Overlay::Overlay(QWidget* parent) : QWidget(parent)
{
    setPalette(Qt::transparent);
    setAttribute(Qt::WA_TransparentForMouseEvents);
}

void Overlay::paintEvent(QPaintEvent* event)
{
    QPainter p(this);
    p.drawText(rect(), Qt::AlignCenter, "Some foo goes here");
}

TrackerWidgetVideo.h

class TrackerWidgetVideo : public VlcWidgetVideo
{
    Q_OBJECT

 public:
    explicit VlcWidgetVideo(QWidget* parent = NULL);

 protected:
    void resizeEvent(QResizeEvent* event);

 private:
    Overlay* overlay;
};

TrackerWidgetVideo.cpp

TrackerWidgetVideo::TrackerWidgetVideo(QWidget* parent) : VlcWidgetVideo(parent)
{
    overlay = new Overlay(this);
}

void TrackerWidgetVideo::resizeEvent(QResizeEvent* event)
{
    overlay->resize(event->size());
    event->accept();
}

Vlc creates two "internal" widgets on VlcVideoWidget when video is playing. Create a new widget as the VlcVideoWidget's sibling(not child), bring it to front and paint on it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!