Painting over a QVideoWidget



  • I'm trying to paint on a qvideowidget (drawing text now, maybe need something more complex later).

    However, when subclassing qvideowidget and overriding the paint event, the text is not painted. Is there another way to paint on top of a qvideowidget?

    This is the code I have so far:

    #include "videow.h"
    
    videoW::videoW(QString dir, QString listFile) {
        player = new QMediaPlayer(this);
        playlist = new QMediaPlaylist(player);
        updatePlayList(dir);
        player->setMedia(playlist);
        player->setPlaybackRate(1);
        player->setVideoOutput(this);
    
    }
    
    void videoW::paintEvent(QPaintEvent *event){
        qDebug() << "Painting";
        QPainter p(this);
        p.drawText(QRect(200,0,1000,200), "Fwejaf;ljflawejf;wj wkjfklweajfl w");
    
    }
    
    void videoW::play(){
        std::cout << "Start playing" << std::endl;
        std::cout << playlist->currentIndex();
        player->play();
    }
    
    void videoW::updatePlayList(QString dir){
        QDir recoredDir(dir);
        QStringList filters{"*.mp4", "*.avi"};
        QStringList filesToDisp = recoredDir.entryList(filters);
        bool getNames = videoNames.isEmpty();
        for (int i = 0; i < filesToDisp.size(); ++i) {
            QString fileName = filesToDisp[i];
            if (isSupported(fileName) && ((!videoNames.isEmpty() && videoNames.contains(fileName)) || getNames)) {
                QMediaResource c(QUrl::fromLocalFile(dir + "/" +fileName));
                playlist->addMedia(c);
                if (getNames){
                    videoNames.append(fileName);
                }
            }
        }
    
        std::cout << "Loaded " << playlist->mediaCount() << " videos for display" << std::endl;
    }
    
    /**
     * @brief videoDisplay::isSupported - check if a file can be played
     * @param name
     * @return
     */
    bool videoW::isSupported(QString name) {
        if (name.indexOf(".avi") > -1 || name.indexOf(".mp4") > -1) {
            return true;
        }
        return false;
    }
    

  • Qt Champions 2017

    Hi
    Does the video show ? as you dont seems to call base PaintEvent.
    Anyway, i was wondering.

    If you overlay say a QLabel. Does that work ?
    If not critical to actually draw yourself.

    also, this seems to work
    https://forum.qt.io/topic/1024/paint-over-qvideowidget/9



  • I've tried with a label, but I was struggling with making it transparent. Will try the link you mentioned, thanks



  • Thanks @mrjj , your overlay tip got me looking for that term and I found https://stackoverflow.com/questions/19199863/draw-rectangular-overlay-on-qwidget-at-click

    Took the second answer, and its working.


  • Qt Champions 2017

    @Jonathan-Levanon
    Good found. nice overlay class :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.