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. Can't draw rectangle on a Custom Video Widget with Paint Event
Forum Update on Monday, May 27th 2025

Can't draw rectangle on a Custom Video Widget with Paint Event

Scheduled Pinned Locked Moved Unsolved General and Desktop
qrectqvideowidgetqpainter
20 Posts 4 Posters 5.2k 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.
  • M mrjj
    28 Oct 2018, 20:10

    @onurcevik
    Hi
    Are you running live video also on the widget ?
    Code looks fine.
    You can try with fixed rect to see if can draw at all.
    painter.drawRect(QRect(0,0,200,200));

    O Offline
    O Offline
    onurcevik
    wrote on 28 Oct 2018, 20:18 last edited by onurcevik
    #8

    @mrjj said in Can't draw rectangle on a Custom Video Widget with Paint Event:

    painter.drawRect(QRect(0,0,200,200));

    I do. Even with fixed rect code it doesn't draw at all. But 5 min ago when I clicked run button with no change in code a red square appeared for a sec then disappeared.!

    Here is a screenshot of current problem this is without using rubberBand->hide();

    M 1 Reply Last reply 28 Oct 2018, 20:39
    0
    • O onurcevik
      28 Oct 2018, 20:18

      @mrjj said in Can't draw rectangle on a Custom Video Widget with Paint Event:

      painter.drawRect(QRect(0,0,200,200));

      I do. Even with fixed rect code it doesn't draw at all. But 5 min ago when I clicked run button with no change in code a red square appeared for a sec then disappeared.!

      Here is a screenshot of current problem this is without using rubberBand->hide();

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Oct 2018, 20:39 last edited by
      #9

      @onurcevik
      I wonder about all the QPainter warnings you get.

      If you remove
      QVideoWidget::paintEvent(ev);

      does it then work ?

      O 1 Reply Last reply 28 Oct 2018, 20:50
      0
      • M mrjj
        28 Oct 2018, 20:39

        @onurcevik
        I wonder about all the QPainter warnings you get.

        If you remove
        QVideoWidget::paintEvent(ev);

        does it then work ?

        O Offline
        O Offline
        onurcevik
        wrote on 28 Oct 2018, 20:50 last edited by
        #10

        @mrjj nope it changes nothing :(

        M 1 Reply Last reply 28 Oct 2018, 21:09
        0
        • O onurcevik
          28 Oct 2018, 20:50

          @mrjj nope it changes nothing :(

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 28 Oct 2018, 21:09 last edited by
          #11

          @onurcevik
          Then something else is wrong.
          This must paint something or we have issue else where

          void MyVideoObject::paintEvent(QPaintEvent *ev)
          {
            //   QVideoWidget::paintEvent(ev);
              QPainter painter(this);    
              painter.setBrush(Qt::red);
             painter.drawRect(QRect(0,0,100,100));
          }
          
          O 1 Reply Last reply 28 Oct 2018, 21:13
          0
          • M mrjj
            28 Oct 2018, 21:09

            @onurcevik
            Then something else is wrong.
            This must paint something or we have issue else where

            void MyVideoObject::paintEvent(QPaintEvent *ev)
            {
              //   QVideoWidget::paintEvent(ev);
                QPainter painter(this);    
                painter.setBrush(Qt::red);
               painter.drawRect(QRect(0,0,100,100));
            }
            
            O Offline
            O Offline
            onurcevik
            wrote on 28 Oct 2018, 21:13 last edited by
            #12

            @mrjj Still no rectangle. Problem is probably at somewhere else like you said

            M 1 Reply Last reply 28 Oct 2018, 21:26
            0
            • O onurcevik
              28 Oct 2018, 21:13

              @mrjj Still no rectangle. Problem is probably at somewhere else like you said

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 28 Oct 2018, 21:26 last edited by
              #13

              @onurcevik
              I was wondering when you do
              // QVideoWidget::paintEvent(ev);
              does video stop being showed ?

              O 1 Reply Last reply 28 Oct 2018, 21:36
              0
              • M mrjj
                28 Oct 2018, 21:26

                @onurcevik
                I was wondering when you do
                // QVideoWidget::paintEvent(ev);
                does video stop being showed ?

                O Offline
                O Offline
                onurcevik
                wrote on 28 Oct 2018, 21:36 last edited by
                #14

                @mrjj no It didn't . I do the video selecting and playing part on mainwindow.cpp and mainwindow.h which I didn't share here but if you want to take a look at here is the pastebin link : https://pastebin.com/1rk7GJc9

                M 1 Reply Last reply 28 Oct 2018, 22:01
                0
                • O onurcevik
                  28 Oct 2018, 21:36

                  @mrjj no It didn't . I do the video selecting and playing part on mainwindow.cpp and mainwindow.h which I didn't share here but if you want to take a look at here is the pastebin link : https://pastebin.com/1rk7GJc9

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 28 Oct 2018, 22:01 last edited by
                  #15

                  @onurcevik
                  Im starting to think the video is not actually drawn via paintEvent but is an overlay
                  drawn on top of the widget.
                  Searching forum showed post where poster could not get paintEvent to work either.
                  https://forum.qt.io/topic/1024/paint-over-qvideowidget/9

                  Also, adding an overlay widget to it seems not to work either.
                  But we should make a fast test to try it

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

                  does this show?

                  O 1 Reply Last reply 29 Oct 2018, 06:21
                  0
                  • M mrjj
                    28 Oct 2018, 22:01

                    @onurcevik
                    Im starting to think the video is not actually drawn via paintEvent but is an overlay
                    drawn on top of the widget.
                    Searching forum showed post where poster could not get paintEvent to work either.
                    https://forum.qt.io/topic/1024/paint-over-qvideowidget/9

                    Also, adding an overlay widget to it seems not to work either.
                    But we should make a fast test to try it

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

                    does this show?

                    O Offline
                    O Offline
                    onurcevik
                    wrote on 29 Oct 2018, 06:21 last edited by
                    #16

                    @mrjj Sorry for late reply. Sadly It did not work again . I added that piece of code in mainwindow.cpp where I select and play video with openfiledialog. like this:

                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                    
                        mediaPlayer = new QMediaPlayer(this);
                    
                        videoObject = new MyVideoObject(this);
                        ui->horizontalLayout_5->addWidget(videoObject);
                        videoObject->resize(500,400);
                        mediaPlayer->setVideoOutput(videoObject);
                        QLabel *label = new QLabel(videoObject);
                        label->setText("Text over video!");
                        label->raise();
                    
                    
                    
                    }
                    

                    My guess is for some reason video always appears in front of label maybe ?

                    M 1 Reply Last reply 29 Oct 2018, 07:16
                    0
                    • O onurcevik
                      29 Oct 2018, 06:21

                      @mrjj Sorry for late reply. Sadly It did not work again . I added that piece of code in mainwindow.cpp where I select and play video with openfiledialog. like this:

                      MainWindow::MainWindow(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                      
                          mediaPlayer = new QMediaPlayer(this);
                      
                          videoObject = new MyVideoObject(this);
                          ui->horizontalLayout_5->addWidget(videoObject);
                          videoObject->resize(500,400);
                          mediaPlayer->setVideoOutput(videoObject);
                          QLabel *label = new QLabel(videoObject);
                          label->setText("Text over video!");
                          label->raise();
                      
                      
                      
                      }
                      

                      My guess is for some reason video always appears in front of label maybe ?

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 29 Oct 2018, 07:16 last edited by
                      #17

                      @onurcevik

                      Hi
                      Super with test.

                      The video must be an overlay
                      and we cant draw on it.

                      so we could try a top level window and see if that can be over video, or
                      the QGraphicsView way, that was reported to work.

                      O 2 Replies Last reply 29 Oct 2018, 07:18
                      0
                      • M mrjj
                        29 Oct 2018, 07:16

                        @onurcevik

                        Hi
                        Super with test.

                        The video must be an overlay
                        and we cant draw on it.

                        so we could try a top level window and see if that can be over video, or
                        the QGraphicsView way, that was reported to work.

                        O Offline
                        O Offline
                        onurcevik
                        wrote on 29 Oct 2018, 07:18 last edited by
                        #18

                        @mrjj I will try to implement it the QGraphicsView way. I will post the results here :)

                        1 Reply Last reply
                        1
                        • M mrjj
                          29 Oct 2018, 07:16

                          @onurcevik

                          Hi
                          Super with test.

                          The video must be an overlay
                          and we cant draw on it.

                          so we could try a top level window and see if that can be over video, or
                          the QGraphicsView way, that was reported to work.

                          O Offline
                          O Offline
                          onurcevik
                          wrote on 29 Oct 2018, 11:54 last edited by
                          #19

                          @mrjj Hey I implemanted the project the QGraphicsView way. But even though I can draw now. When I open a video I can only hear the audio but can't see the video. Can you help me with this now ?

                          mygraphicsview.h

                          #ifndef MYGRAPHICSVIEW_H
                          #define MYGRAPHICSVIEW_H
                          
                          #include <QObject>
                          #include <QWidget>
                          #include <QGraphicsView>
                          #include <QMouseEvent>
                          #include <QPainter>
                          #include <QRubberBand>
                          #include <QLabel>
                          
                          class MyGraphicsView : public QGraphicsView
                          {
                              Q_OBJECT
                          public:
                              MyGraphicsView(QWidget *parent = nullptr);
                          protected:
                          
                              void mouseMoveEvent(QMouseEvent *ev);
                              void mousePressEvent(QMouseEvent *ev);
                              void mouseReleaseEvent(QMouseEvent *ev);
                              void paintEvent(QPaintEvent *ev);
                          private:
                                 QRubberBand *rubberBand;
                                 QPoint origin;
                                 QRect rect;
                          
                          
                          };
                          
                          #endif // MYGRAPHICSVIEW_H
                          

                          mygraphicsview.cpp

                          #include "mygraphicsview.h"
                          
                          MyGraphicsView::MyGraphicsView(QWidget *parent):
                              QGraphicsView (parent), rubberBand(nullptr){}
                          
                          void MyGraphicsView::mousePressEvent(QMouseEvent *ev)
                          {
                              origin = ev->pos();
                                  if(!rubberBand)
                                      rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
                                  rubberBand->setGeometry(QRect(origin,QSize()));
                                  rubberBand->show();
                                  QGraphicsView::mousePressEvent(ev);
                          
                          }
                          
                          void MyGraphicsView::mouseMoveEvent(QMouseEvent *ev)
                          {
                              rubberBand->setGeometry(QRect(origin,ev->pos()).normalized());
                               QGraphicsView::mouseMoveEvent(ev);
                          
                          }
                          
                          void MyGraphicsView::mouseReleaseEvent(QMouseEvent *ev)
                          {
                              rect = rubberBand->geometry();
                              update();
                               QGraphicsView::mouseReleaseEvent(ev);
                          
                          }
                          
                          void MyGraphicsView::paintEvent(QPaintEvent *ev)
                          {
                          
                              QGraphicsView::paintEvent(ev);
                              QPainter painter(this->viewport());
                          
                              painter.setBrush(Qt::red);
                               if(!rect.isNull())
                                  painter.drawRect(rect);
                          
                          }
                          
                          C 1 Reply Last reply 1 Jul 2019, 19:34
                          1
                          • O onurcevik
                            29 Oct 2018, 11:54

                            @mrjj Hey I implemanted the project the QGraphicsView way. But even though I can draw now. When I open a video I can only hear the audio but can't see the video. Can you help me with this now ?

                            mygraphicsview.h

                            #ifndef MYGRAPHICSVIEW_H
                            #define MYGRAPHICSVIEW_H
                            
                            #include <QObject>
                            #include <QWidget>
                            #include <QGraphicsView>
                            #include <QMouseEvent>
                            #include <QPainter>
                            #include <QRubberBand>
                            #include <QLabel>
                            
                            class MyGraphicsView : public QGraphicsView
                            {
                                Q_OBJECT
                            public:
                                MyGraphicsView(QWidget *parent = nullptr);
                            protected:
                            
                                void mouseMoveEvent(QMouseEvent *ev);
                                void mousePressEvent(QMouseEvent *ev);
                                void mouseReleaseEvent(QMouseEvent *ev);
                                void paintEvent(QPaintEvent *ev);
                            private:
                                   QRubberBand *rubberBand;
                                   QPoint origin;
                                   QRect rect;
                            
                            
                            };
                            
                            #endif // MYGRAPHICSVIEW_H
                            

                            mygraphicsview.cpp

                            #include "mygraphicsview.h"
                            
                            MyGraphicsView::MyGraphicsView(QWidget *parent):
                                QGraphicsView (parent), rubberBand(nullptr){}
                            
                            void MyGraphicsView::mousePressEvent(QMouseEvent *ev)
                            {
                                origin = ev->pos();
                                    if(!rubberBand)
                                        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
                                    rubberBand->setGeometry(QRect(origin,QSize()));
                                    rubberBand->show();
                                    QGraphicsView::mousePressEvent(ev);
                            
                            }
                            
                            void MyGraphicsView::mouseMoveEvent(QMouseEvent *ev)
                            {
                                rubberBand->setGeometry(QRect(origin,ev->pos()).normalized());
                                 QGraphicsView::mouseMoveEvent(ev);
                            
                            }
                            
                            void MyGraphicsView::mouseReleaseEvent(QMouseEvent *ev)
                            {
                                rect = rubberBand->geometry();
                                update();
                                 QGraphicsView::mouseReleaseEvent(ev);
                            
                            }
                            
                            void MyGraphicsView::paintEvent(QPaintEvent *ev)
                            {
                            
                                QGraphicsView::paintEvent(ev);
                                QPainter painter(this->viewport());
                            
                                painter.setBrush(Qt::red);
                                 if(!rect.isNull())
                                    painter.drawRect(rect);
                            
                            }
                            
                            C Offline
                            C Offline
                            CurvedMoose
                            wrote on 1 Jul 2019, 19:34 last edited by
                            #20

                            @onurcevik Did you ever figure the solution for this? Working on it now

                            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