<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event]]></title><description><![CDATA[<p dir="auto">For a GUI project I want to select an area on a custom video widget and draw rectangle on selected area.</p>
<p dir="auto">So far with the code down below I can select the area with QRubberband but instead of drawing red rectangle when I release the click the code just stops QRubberband from disappearing.</p>
<p dir="auto">Here is the code:</p>
<p dir="auto"><strong>myvideoobject.h</strong></p>
<pre><code>#ifndef MYVIDEOOBJECT_H
#define MYVIDEOOBJECT_H

#include &lt;QVideoWidget&gt;

class QRubberBand;

class MyVideoObject : public QVideoWidget
{
public:
    MyVideoObject(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 // MYVIDEOOBJECT_H
</code></pre>
<p dir="auto"><strong>myvideoobject.cpp</strong></p>
<pre><code>#include "myvideoobject.h"

#include &lt;QMouseEvent&gt;
#include &lt;QPainter&gt;
#include &lt;QRubberBand&gt;

MyVideoObject::MyVideoObject(QWidget *parent):
    QVideoWidget(parent),
    rubberBand(nullptr){}

void MyVideoObject::mousePressEvent(QMouseEvent *ev)
{
    origin = ev-&gt;pos();
    if(!rubberBand)
        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    rubberBand-&gt;setGeometry(QRect(origin,QSize()));
    rubberBand-&gt;show();
    QVideoWidget::mousePressEvent(ev);
}

void MyVideoObject::mouseMoveEvent(QMouseEvent *ev)
{
    rubberBand-&gt;setGeometry(QRect(origin,ev-&gt;pos()).normalized());
    QVideoWidget::mouseMoveEvent(ev);
}

void MyVideoObject::mouseReleaseEvent(QMouseEvent *ev)
{
    rect = rubberBand-&gt;geometry();
    update();
    QVideoWidget::mouseReleaseEvent(ev);
}

void MyVideoObject::paintEvent(QPaintEvent *ev)
{
    QVideoWidget::paintEvent(ev);
    QPainter painter(this);
    painter.save();
    painter.setBrush(Qt::red);
    if(!rect.isNull())
        painter.drawRect(rect);
    painter.restore();
}
</code></pre>
<p dir="auto">So how can I fix this problem ?</p>
]]></description><link>https://forum.qt.io/topic/96035/can-t-draw-rectangle-on-a-custom-video-widget-with-paint-event</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 21:03:01 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/96035.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 28 Oct 2018 17:35:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Mon, 01 Jul 2019 19:34:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/onurcevik">@<bdi>onurcevik</bdi></a> Did you ever figure the solution for this? Working on it now</p>
]]></description><link>https://forum.qt.io/post/538476</link><guid isPermaLink="true">https://forum.qt.io/post/538476</guid><dc:creator><![CDATA[CurvedMoose]]></dc:creator><pubDate>Mon, 01 Jul 2019 19:34:46 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Mon, 29 Oct 2018 11:54:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> 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 ?</p>
<p dir="auto"><strong>mygraphicsview.h</strong></p>
<pre><code>#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include &lt;QObject&gt;
#include &lt;QWidget&gt;
#include &lt;QGraphicsView&gt;
#include &lt;QMouseEvent&gt;
#include &lt;QPainter&gt;
#include &lt;QRubberBand&gt;
#include &lt;QLabel&gt;

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
</code></pre>
<p dir="auto"><strong>mygraphicsview.cpp</strong></p>
<pre><code>#include "mygraphicsview.h"

MyGraphicsView::MyGraphicsView(QWidget *parent):
    QGraphicsView (parent), rubberBand(nullptr){}

void MyGraphicsView::mousePressEvent(QMouseEvent *ev)
{
    origin = ev-&gt;pos();
        if(!rubberBand)
            rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
        rubberBand-&gt;setGeometry(QRect(origin,QSize()));
        rubberBand-&gt;show();
        QGraphicsView::mousePressEvent(ev);

}

void MyGraphicsView::mouseMoveEvent(QMouseEvent *ev)
{
    rubberBand-&gt;setGeometry(QRect(origin,ev-&gt;pos()).normalized());
     QGraphicsView::mouseMoveEvent(ev);

}

void MyGraphicsView::mouseReleaseEvent(QMouseEvent *ev)
{
    rect = rubberBand-&gt;geometry();
    update();
     QGraphicsView::mouseReleaseEvent(ev);

}

void MyGraphicsView::paintEvent(QPaintEvent *ev)
{

    QGraphicsView::paintEvent(ev);
    QPainter painter(this-&gt;viewport());

    painter.setBrush(Qt::red);
     if(!rect.isNull())
        painter.drawRect(rect);

}
</code></pre>
]]></description><link>https://forum.qt.io/post/489748</link><guid isPermaLink="true">https://forum.qt.io/post/489748</guid><dc:creator><![CDATA[onurcevik]]></dc:creator><pubDate>Mon, 29 Oct 2018 11:54:12 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Mon, 29 Oct 2018 07:18:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> I will try to implement it the QGraphicsView way. I will post the results here :)</p>
]]></description><link>https://forum.qt.io/post/489686</link><guid isPermaLink="true">https://forum.qt.io/post/489686</guid><dc:creator><![CDATA[onurcevik]]></dc:creator><pubDate>Mon, 29 Oct 2018 07:18:55 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Mon, 29 Oct 2018 07:16:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/onurcevik">@<bdi>onurcevik</bdi></a></p>
<p dir="auto">Hi<br />
Super with test.</p>
<p dir="auto">The video must be an overlay<br />
and we cant draw on it.</p>
<p dir="auto">so we could try a top level window and see if that can be over video, or<br />
the QGraphicsView way, that was reported to work.</p>
]]></description><link>https://forum.qt.io/post/489684</link><guid isPermaLink="true">https://forum.qt.io/post/489684</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Mon, 29 Oct 2018 07:16:58 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Mon, 29 Oct 2018 06:21:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> 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:</p>
<pre><code>MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    mediaPlayer = new QMediaPlayer(this);

    videoObject = new MyVideoObject(this);
    ui-&gt;horizontalLayout_5-&gt;addWidget(videoObject);
    videoObject-&gt;resize(500,400);
    mediaPlayer-&gt;setVideoOutput(videoObject);
    QLabel *label = new QLabel(videoObject);
    label-&gt;setText("Text over video!");
    label-&gt;raise();



}
</code></pre>
<p dir="auto">My guess is for some reason video always appears in front of label maybe ?</p>
]]></description><link>https://forum.qt.io/post/489661</link><guid isPermaLink="true">https://forum.qt.io/post/489661</guid><dc:creator><![CDATA[onurcevik]]></dc:creator><pubDate>Mon, 29 Oct 2018 06:21:58 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 22:01:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/onurcevik">@<bdi>onurcevik</bdi></a><br />
Im starting to think the video is not actually drawn via paintEvent but is an overlay<br />
drawn on top of the widget.<br />
Searching forum showed post where poster could not get paintEvent to work either.<br />
<a href="https://forum.qt.io/topic/1024/paint-over-qvideowidget/9">https://forum.qt.io/topic/1024/paint-over-qvideowidget/9</a></p>
<p dir="auto">Also, adding an overlay widget to it seems not to work either.<br />
But we should make a fast test to try it</p>
<pre><code>QLabel *label = new QLabel(MyVideoObject);
label-&gt;setText("Text over video!");
label-&gt;raise();
</code></pre>
<p dir="auto">does this show?</p>
]]></description><link>https://forum.qt.io/post/489607</link><guid isPermaLink="true">https://forum.qt.io/post/489607</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 28 Oct 2018 22:01:59 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 21:36:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> 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 : <a href="https://pastebin.com/1rk7GJc9" target="_blank" rel="noopener noreferrer nofollow ugc">https://pastebin.com/1rk7GJc9</a></p>
]]></description><link>https://forum.qt.io/post/489598</link><guid isPermaLink="true">https://forum.qt.io/post/489598</guid><dc:creator><![CDATA[onurcevik]]></dc:creator><pubDate>Sun, 28 Oct 2018 21:36:58 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 21:26:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/onurcevik">@<bdi>onurcevik</bdi></a><br />
I was wondering when you do<br />
//   QVideoWidget::paintEvent(ev);<br />
does video stop being showed ?</p>
]]></description><link>https://forum.qt.io/post/489597</link><guid isPermaLink="true">https://forum.qt.io/post/489597</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 28 Oct 2018 21:26:47 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 21:13:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> Still no rectangle. Problem is probably at somewhere else like you said</p>
]]></description><link>https://forum.qt.io/post/489595</link><guid isPermaLink="true">https://forum.qt.io/post/489595</guid><dc:creator><![CDATA[onurcevik]]></dc:creator><pubDate>Sun, 28 Oct 2018 21:13:19 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 21:09:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/onurcevik">@<bdi>onurcevik</bdi></a><br />
Then something else is wrong.<br />
This must paint something or we have issue else where</p>
<pre><code>void MyVideoObject::paintEvent(QPaintEvent *ev)
{
  //   QVideoWidget::paintEvent(ev);
    QPainter painter(this);    
    painter.setBrush(Qt::red);
   painter.drawRect(QRect(0,0,100,100));
}
</code></pre>
]]></description><link>https://forum.qt.io/post/489594</link><guid isPermaLink="true">https://forum.qt.io/post/489594</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 28 Oct 2018 21:09:58 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 20:50:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> nope it changes nothing :(</p>
]]></description><link>https://forum.qt.io/post/489591</link><guid isPermaLink="true">https://forum.qt.io/post/489591</guid><dc:creator><![CDATA[onurcevik]]></dc:creator><pubDate>Sun, 28 Oct 2018 20:50:52 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 20:39:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/onurcevik">@<bdi>onurcevik</bdi></a><br />
I wonder about all the QPainter warnings you get.</p>
<p dir="auto">If you remove<br />
QVideoWidget::paintEvent(ev);</p>
<p dir="auto">does it then work ?</p>
]]></description><link>https://forum.qt.io/post/489590</link><guid isPermaLink="true">https://forum.qt.io/post/489590</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 28 Oct 2018 20:39:09 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 20:19:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> said in <a href="/post/489584">Can't draw rectangle on a Custom Video Widget with Paint Event</a>:</p>
<blockquote>
<p dir="auto">painter.drawRect(QRect(0,0,200,200));</p>
</blockquote>
<p dir="auto">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.!</p>
<p dir="auto"><a href="https://imgur.com/a/RL9VACO" target="_blank" rel="noopener noreferrer nofollow ugc">Here is a screenshot of current problem</a> this is without using rubberBand-&gt;hide();</p>
]]></description><link>https://forum.qt.io/post/489587</link><guid isPermaLink="true">https://forum.qt.io/post/489587</guid><dc:creator><![CDATA[onurcevik]]></dc:creator><pubDate>Sun, 28 Oct 2018 20:19:07 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 20:10:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/onurcevik">@<bdi>onurcevik</bdi></a><br />
Hi<br />
Are you running live video also on the widget ?<br />
Code looks fine.<br />
You can try with fixed rect to see if can draw at all.<br />
painter.drawRect(QRect(0,0,200,200));</p>
]]></description><link>https://forum.qt.io/post/489584</link><guid isPermaLink="true">https://forum.qt.io/post/489584</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 28 Oct 2018 20:10:30 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 20:06:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> sadly thats not the only problem. It does hide the QRubberband but the  main problem is void <code>MyVideoObject::paintEvent(QPaintEvent *ev)</code> doesnt work. So no rectangle is drawn by that method.</p>
]]></description><link>https://forum.qt.io/post/489582</link><guid isPermaLink="true">https://forum.qt.io/post/489582</guid><dc:creator><![CDATA[onurcevik]]></dc:creator><pubDate>Sun, 28 Oct 2018 20:06:19 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 19:31:31 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
As i read your post, your issue is that QRubberBand stays when you release mouse button?<br />
If yes to that, you just need<br />
rubberBand-&gt;hide();<br />
in mouseReleaseEvent</p>
]]></description><link>https://forum.qt.io/post/489576</link><guid isPermaLink="true">https://forum.qt.io/post/489576</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 28 Oct 2018 19:31:31 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 19:29:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/onurcevik">@<bdi>onurcevik</bdi></a> said in <a href="/post/489566">Can't draw rectangle on a Custom Video Widget with Paint Event</a>:</p>
<blockquote>
<p dir="auto">How can I make rect invalid?</p>
</blockquote>
<p dir="auto">rect = QRect()</p>
]]></description><link>https://forum.qt.io/post/489574</link><guid isPermaLink="true">https://forum.qt.io/post/489574</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sun, 28 Oct 2018 19:29:03 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 18:03:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> How can I make rect invalid? Btw my problem is basically I want to draw a red rectangle on selected area but QRubberband Selection Rectangle doesn't disappear. Sorry for my poor English :)</p>
]]></description><link>https://forum.qt.io/post/489566</link><guid isPermaLink="true">https://forum.qt.io/post/489566</guid><dc:creator><![CDATA[onurcevik]]></dc:creator><pubDate>Sun, 28 Oct 2018 18:03:01 GMT</pubDate></item><item><title><![CDATA[Reply to Can&#x27;t draw rectangle on a Custom Video Widget with Paint Event on Sun, 28 Oct 2018 17:43:48 GMT]]></title><description><![CDATA[<p dir="auto">I'm not sure I understand your problem but if you won't draw the rectangle after the mouseReleaseEvent you should make your rect invalid so your check in paintEvent() (!rect.isNull()) will jump in.</p>
]]></description><link>https://forum.qt.io/post/489563</link><guid isPermaLink="true">https://forum.qt.io/post/489563</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sun, 28 Oct 2018 17:43:48 GMT</pubDate></item></channel></rss>