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
QtWS25 Last Chance

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.1k 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.
  • O onurcevik

    @mrjj sadly thats not the only problem. It does hide the QRubberband but the main problem is void MyVideoObject::paintEvent(QPaintEvent *ev) doesnt work. So no rectangle is drawn by that method.

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #7

    @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 1 Reply Last reply
    0
    • mrjjM mrjj

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

      mrjjM 1 Reply Last reply
      0
      • O onurcevik

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

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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
        0
        • mrjjM mrjj

          @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 last edited by
          #10

          @mrjj nope it changes nothing :(

          mrjjM 1 Reply Last reply
          0
          • O onurcevik

            @mrjj nope it changes nothing :(

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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
            0
            • mrjjM mrjj

              @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 last edited by
              #12

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

              mrjjM 1 Reply Last reply
              0
              • O onurcevik

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #13

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

                O 1 Reply Last reply
                0
                • mrjjM mrjj

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

                  O Offline
                  O Offline
                  onurcevik
                  wrote on 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

                  mrjjM 1 Reply Last reply
                  0
                  • O onurcevik

                    @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

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 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
                    0
                    • mrjjM mrjj

                      @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 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 ?

                      mrjjM 1 Reply Last reply
                      0
                      • O onurcevik

                        @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 ?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 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
                        0
                        • mrjjM mrjj

                          @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 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
                          • mrjjM mrjj

                            @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 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
                            • O onurcevik

                              @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 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