How to draw rectangle on QgraphicsView playing video?



  • I want to draw a rectangle on graphics-view. Graphics-View is playing video. Video stream is live. Its not from storage or database.

    Please help.


  • Lifetime Qt Champion

    Hi,

    You should give more details about what you are using to display the video.
    What did you try ? How did it fail ?



  • actually i want to draw rectangle on live streaming and my live stream play on Graphics view, and second i take graphics scene for draw on graphics view then my paint event work properly but always video on top surface and my paint event run on background of video.
    my code..............................................

    customGraphicsvVew.h
    #ifndef CUSTOMGRAPHICSVIEW_H
    #define CUSTOMGRAPHICSVIEW_H

    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsItem>

    class CustomGraphicsView : public QGraphicsView
    {
    Q_OBJECT
    private:
    QGraphicsScene * m_overlayScene;
    public:
    // explicit CustomGraphicsView();

    explicit CustomGraphicsView(QWidget* parent = 0);
    explicit CustomGraphicsView(QGraphicsScene * scene = 0, QWidget * parent = 0);
    
    void setOverlayScene(QGraphicsScene * scene);
    QGraphicsScene * overlayScene();
    virtual void paintOverlay();
    void paintEvent(QPaintEvent *ev);
    

    public slots:
    void overlayChanged();
    };

    #endif // CUSTOMGRAPHICSVIEW_H

    #include "customGraphicsView.h"

    CustomGraphicsView::CustomGraphicsView(QWidget* parent): QGraphicsView(parent)
    {
    m_overlayScene = NULL;
    }

    CustomGraphicsView::CustomGraphicsView(QGraphicsScene * scene, QWidget * parent): QGraphicsView(scene,parent)
    {
    m_overlayScene = NULL;
    }

    void CustomGraphicsView::setOverlayScene(QGraphicsScene * scene)
    {
    if (scene == m_overlayScene) return;
    m_overlayScene = scene;
    connect(scene, SIGNAL(changed(QList<QRectF>)), SLOT(overlayChanged()));
    update();
    }

    QGraphicsScene * CustomGraphicsView::overlayScene()
    {
    return m_overlayScene;
    }

    void CustomGraphicsView::paintEvent(QPaintEvent *ev)
    {
    QGraphicsView::paintEvent(ev);
    if (m_overlayScene) paintOverlay();
    }

    void CustomGraphicsView::paintOverlay()
    {
    QPainter p(viewport());
    p.setRenderHints(renderHints());
    m_overlayScene->render(&p, viewport()->rect());
    }

    void CustomGraphicsView::overlayChanged()
    {
    update();
    }
    ///////////////////////////////////////////

    customScene.h
    #ifndef CUSTOMSCENE_H
    #define CUSTOMSCENE_H

    #include <QGraphicsScene>
    #include <QGraphicsSceneMouseEvent>
    #include <QGraphicsLineItem>
    #include <QAction>
    #include <QGraphicsView>
    #include <QKeyEvent>
    #include<QDebug>
    #include <QMouseEventTransition>

    class CustomScene : public QGraphicsScene
    {
    public:
    enum CustomSceneMode {NoMode, SelectObject, DrawRect};
    CustomScene(QObject* parent = 0);
    void setMode(CustomSceneMode mode);
    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void keyPressEvent(QKeyEvent event);
    private:
    CustomSceneMode sceneMode;
    QPointF origPoint;
    QGraphicsRectItem
    itemToDraw;
    void makeItemsControllable(bool areControllable);
    int m_NumOfItemsToDraw;
    };

    #include "customScene.h"

    CustomScene::CustomScene(QObject *parent): QGraphicsScene(parent)
    {
    sceneMode = NoMode;
    itemToDraw = 0;
    m_NumOfItemsToDraw = 4;
    // setMode(DrawRect);

    }

    void CustomScene::setMode(CustomSceneMode mode){
    sceneMode = mode;
    QGraphicsView::DragMode vMode =
    QGraphicsView::NoDrag;
    if(mode == DrawRect){
    makeItemsControllable(false);
    vMode = QGraphicsView::NoDrag;
    }
    else if(mode == SelectObject){
    makeItemsControllable(true);
    vMode = QGraphicsView::RubberBandDrag;
    }
    QGraphicsView* mView = views().at(0);
    if(mView)
    {
    mView->setDragMode(vMode);
    }

    update();
    

    }

    void CustomScene::makeItemsControllable(bool areControllable){
    foreach(QGraphicsItem* item, items()){
    item->setFlag(QGraphicsItem::ItemIsSelectable,
    areControllable);
    item->setFlag(QGraphicsItem::ItemIsMovable,
    areControllable);
    qDebug()<< item->type();
    }
    update();
    }

    void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent *event){
    if((sceneMode == DrawRect) && (m_NumOfItemsToDraw >= items().count()))
    {
    origPoint = event->scenePos();
    update();
    }
    else
    {
    makeItemsControllable(true);
    setMode(SelectObject);
    update();
    }
    QGraphicsScene::mousePressEvent(event);
    }

    void CustomScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
    if((sceneMode == DrawRect)&& (m_NumOfItemsToDraw >= items().count())){
    if(!itemToDraw){
    itemToDraw = new QGraphicsRectItem;
    itemToDraw->setZValue(1000.00);
    this->addItem(itemToDraw);
    itemToDraw->setPen(QPen(Qt::black, 3, Qt::SolidLine));
    itemToDraw->setPos(origPoint);
    itemToDraw->setActive(true);
    }
    itemToDraw->setRect(0,0,
    event->scenePos().x() - origPoint.x(),
    event->scenePos().y() - origPoint.y());

        update();
    }
    else
    {
        QGraphicsScene::mouseMoveEvent(event);
    }
    

    }

    void CustomScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    itemToDraw = 0;
    update();
    QGraphicsScene::mouseReleaseEvent(event);
    }

    void CustomScene::keyPressEvent(QKeyEvent *event){
    if(m_NumOfItemsToDraw >= items().count())
    {
    setMode(DrawRect);
    }

    if(event->key() == Qt::Key_Delete)
    {
        foreach(QGraphicsItem* item, selectedItems()){
            removeItem(item);
            delete item;
        }
    }
    else
    {
        QGraphicsScene::keyPressEvent(event);
    }
    
    update();
    

    }

    #endif // CUSTOMSCENE_H
    ///////////////////////////////////////////////////////

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "customScene.h"
    #include "customGraphicsView.h"
    #include <QtWidgets/QApplication>
    #include <QGraphicsScene>
    #include <QtWidgets/QApplication>
    #include <QGraphicsView>

    qreal rnd() { return qrand() / (float)RAND_MAX; }

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    this->setMouseTracking(true);

    CustomScene *scene = new CustomScene();
    ui->graphicsView->setScene(scene);
    scene->setMode(CustomScene::DrawRect);
    
    
    QGraphicsScene *scene1 = new QGraphicsScene();
    CustomGraphicsView *gview = new CustomGraphicsView(scene1);
    
    for (int i = 0; i < 20; ++ i) {
        qreal w = rnd()*0.3, h = rnd()*0.3;
        scene1->addEllipse(rnd()*(1-w), rnd()*(1-h), w, h, QPen(Qt::red), QBrush(Qt::lightGray));
    }
    
    QPainter p(ui->graphicsView->viewport());
    p.setRenderHints(ui->graphicsView->renderHints());
    scene->render(&p, ui->graphicsView->viewport()->rect());
    

    }

    void MainWindow::on_pushButton_clicked()
    {
    qDebug()<<"Clicked Play";
    // m_pMediaPlay = new MediaPlayer((void*)ui->graphicsView->winId(),ui->graphicsView->width(), ui->graphicsView->height());
    // m_pMediaPlay->SetUrl("rtsp://root:pass@10.103.3.6/axis-media/media.amp");
    // (*m_pMediaPlay)();
    }
    MainWindow::~MainWindow()
    {
    delete ui;
    }

    ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QLabel>

    //#include"mediaPlayer.hpp"
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:

    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    //MediaPlayer *m_pMediaPlay;

    };

    #endif // MAINWINDOW_H

    //////////////////////////////////////////////////
    main.cpp
    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
    

    }


  • Lifetime Qt Champion

    What's that MediaPlayer class ?



  • Mediaplayer is instance of Player.


  • Lifetime Qt Champion

    And what is Player ?



  • atually i used SDL2 and ffmpeg lib with STL for Player.



  • you have a any idea about how to draw rectangle on live Streaming???



  • @SGaist

    atually i used SDL2 and ffmpeg lib with STL for Player.


  • Lifetime Qt Champion

    That's the kind of "small details" that you should mentioned from start. Your combo might just make thing pretty hard to do. I don't know how your Player class is working so I can't comment on the feasibility of an overlay like you are trying to implement.


Log in to reply
 

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