Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to draw rectangle on QgraphicsView playing video?

How to draw rectangle on QgraphicsView playing video?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mit_cruzeM Offline
    mit_cruzeM Offline
    mit_cruze
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • mit_cruzeM Offline
        mit_cruzeM Offline
        mit_cruze
        wrote on last edited by
        #3

        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();
        

        }

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What's that MediaPlayer class ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • mit_cruzeM Offline
            mit_cruzeM Offline
            mit_cruze
            wrote on last edited by
            #5

            Mediaplayer is instance of Player.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              And what is Player ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              mit_cruzeM 1 Reply Last reply
              0
              • mit_cruzeM Offline
                mit_cruzeM Offline
                mit_cruze
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • mit_cruzeM Offline
                  mit_cruzeM Offline
                  mit_cruze
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    And what is Player ?

                    mit_cruzeM Offline
                    mit_cruzeM Offline
                    mit_cruze
                    wrote on last edited by
                    #9

                    @SGaist

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

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved