Can't draw rectangle on a Custom Video Widget with Paint Event
-
@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)); -
@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();
-
@onurcevik
I wonder about all the QPainter warnings you get.If you remove
QVideoWidget::paintEvent(ev);does it then work ?
-
@onurcevik
Then something else is wrong.
This must paint something or we have issue else wherevoid MyVideoObject::paintEvent(QPaintEvent *ev) { // QVideoWidget::paintEvent(ev); QPainter painter(this); painter.setBrush(Qt::red); painter.drawRect(QRect(0,0,100,100)); }
-
@onurcevik
I was wondering when you do
// QVideoWidget::paintEvent(ev);
does video stop being showed ? -
@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/9Also, adding an overlay widget to it seems not to work either.
But we should make a fast test to try itQLabel *label = new QLabel(MyVideoObject); label->setText("Text over video!"); label->raise();
does this show?
-
@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 ?
-
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. -
@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); }
-
@onurcevik Did you ever figure the solution for this? Working on it now