<?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[QGLWidget issue?!]]></title><description><![CDATA[<p dir="auto">I get really strange artifacts with the following simple application when using <em>QGLWidget</em> and a dashed pen in <em>MyRectItem::paint()</em>:</p>
<p dir="auto">!<a href="http://www.bilderload.com/daten/screenshotSY80M.png(OK)" target="_blank" rel="noopener noreferrer nofollow ugc">http://www.bilderload.com/daten/screenshotSY80M.png(OK)</a>!<br />
!<a href="http://www.bilderload.com/daten/screenshot1KQZWZ.png(Artifacts)" target="_blank" rel="noopener noreferrer nofollow ugc">http://www.bilderload.com/daten/screenshot1KQZWZ.png(Artifacts)</a>!</p>
<p dir="auto"><em>Code:</em></p>
<p dir="auto"><em>dialog.ui</em><br />
@&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />
&lt;ui version="4.0"&gt;<br />
&lt;class&gt;Dialog&lt;/class&gt;<br />
&lt;widget class="QDialog" name="Dialog"&gt;<br />
&lt;property name="geometry"&gt;<br />
&lt;rect&gt;<br />
&lt;x&gt;0&lt;/x&gt;<br />
&lt;y&gt;0&lt;/y&gt;<br />
&lt;width&gt;400&lt;/width&gt;<br />
&lt;height&gt;300&lt;/height&gt;<br />
&lt;/rect&gt;<br />
&lt;/property&gt;<br />
&lt;property name="windowTitle"&gt;<br />
&lt;string&gt;Dialog&lt;/string&gt;<br />
&lt;/property&gt;<br />
&lt;layout class="QHBoxLayout" name="horizontalLayout"&gt;<br />
&lt;item&gt;<br />
&lt;widget class="QGraphicsView" name="graphicsView"/&gt;<br />
&lt;/item&gt;<br />
&lt;/layout&gt;<br />
&lt;/widget&gt;<br />
&lt;layoutdefault spacing="6" margin="11"/&gt;<br />
&lt;resources/&gt;<br />
&lt;connections/&gt;<br />
&lt;/ui&gt;@</p>
<p dir="auto"><em>dialog.h</em><br />
@#ifndef DIALOG_H<br />
#define DIALOG_H</p>
<p dir="auto">#include &lt;QDialog&gt;</p>
<p dir="auto">namespace Ui {<br />
class Dialog;<br />
}</p>
<p dir="auto">class QGraphicsView;<br />
class QGraphicsScene;<br />
class VipuGuiCanvasItem;<br />
class MyRectItem;</p>
<p dir="auto">class Dialog : public QDialog<br />
{<br />
Q_OBJECT</p>
<p dir="auto">public:<br />
explicit Dialog(QWidget *parent = 0);<br />
~Dialog();</p>
<p dir="auto">protected:<br />
bool eventFilter(QObject *pObject, QEvent *pEvent);</p>
<p dir="auto">private:<br />
Ui::Dialog *ui;</p>
<p dir="auto">public:<br />
QGraphicsView     *m_pView;<br />
QGraphicsScene    *m_pScene;<br />
VipuGuiCanvasItem *m_pCanvasItem;<br />
MyRectItem        *m_pMyRectItem;<br />
};</p>
<p dir="auto">#endif // DIALOG_H@</p>
<p dir="auto"><em>dialog.cpp</em><br />
@#include "dialog.h"<br />
#include "ui_dialog.h"<br />
#include &lt;QGLWidget&gt;<br />
#include &lt;QGraphicsView&gt;<br />
#include &lt;QGraphicsScene&gt;<br />
#include &lt;QGraphicsRectItem&gt;<br />
#include &lt;QGraphicsSceneWheelEvent&gt;<br />
#include &lt;QGraphicsObject&gt;</p>
<p dir="auto">class MyRectItem : public QGraphicsObject<br />
{<br />
public:<br />
MyRectItem(QGraphicsItem *pParent = 0) : QGraphicsObject(pParent)<br />
{}<br />
QRectF boundingRect() const<br />
{<br />
return m_oBoundingRect;<br />
}<br />
void paint(QPainter *pPainter, const QStyleOptionGraphicsItem *, QWidget *)<br />
{<br />
QRectF oRect(QPointF(0,0), boundingRect().size());<br />
oRect.moveCenter(boundingRect().center());<br />
oRect.adjust(5, 5, -5, -5);<br />
#if 1 // --&gt; PROBLEM!<br />
QPen oPen(Qt::DashLine);<br />
#else<br />
QPen oPen(Qt::SolidLine);<br />
#endif<br />
oPen.setColor(QColor(255,0,0,255)); // RED<br />
pPainter-&gt;setPen(oPen);<br />
pPainter-&gt;drawRect(oRect);</p>
<pre><code>    oPen.setColor(QColor(0,255,0,255)); // GREEN
    pPainter-&gt;setPen(oPen);
    pPainter-&gt;drawEllipse(oRect);
}
void setRect(const QRectF&amp; oRect)
{
    m_oBoundingRect = oRect;
    update();
}
</code></pre>
<p dir="auto">private:<br />
QRectF m_oBoundingRect;<br />
};</p>
<p dir="auto">class VipuGuiCanvasItem : public QGraphicsRectItem<br />
{<br />
public:<br />
VipuGuiCanvasItem(Dialog *pDialog) : m_pDialog(pDialog)<br />
{}<br />
protected:<br />
void wheelEvent(QGraphicsSceneWheelEvent *pEvent)<br />
{<br />
{<br />
double fScale = pEvent-&gt;delta();<br />
QRectF oRect = m_pDialog-&gt;m_pMyRectItem-&gt;boundingRect();<br />
m_pDialog-&gt;m_pMyRectItem-&gt;setRect(<br />
oRect.adjusted(fScale &gt;= 0. ? -1. :  1.,<br />
fScale &gt;= 0. ? -1. :  1.,<br />
fScale &gt;= 0. ?  1. : -1.,<br />
fScale &gt;= 0. ?  1. : -1.));<br />
}<br />
}<br />
private:<br />
Dialog *m_pDialog;<br />
};</p>
<p dir="auto">Dialog::Dialog(QWidget *parent) :<br />
QDialog(parent),<br />
ui(new Ui::Dialog)<br />
{<br />
ui-&gt;setupUi(this);</p>
<pre><code>m_pView = ui-&gt;graphicsView;
</code></pre>
<p dir="auto">#if 1 // --&gt; PROBLEM!<br />
m_pView-&gt;setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));<br />
#endif<br />
m_pView-&gt;viewport()-&gt;installEventFilter(this);</p>
<pre><code>m_pScene = new QGraphicsScene(this);
m_pView-&gt;setScene(m_pScene);

m_pCanvasItem = new VipuGuiCanvasItem(this);
m_pCanvasItem-&gt;setVisible(true);
m_pCanvasItem-&gt;setPen(Qt::NoPen);
m_pCanvasItem-&gt;setBrush(Qt::NoBrush);
m_pCanvasItem-&gt;setFlag(QGraphicsItem::ItemHasNoContents);
m_pScene-&gt;addItem(m_pCanvasItem);

m_pMyRectItem = new MyRectItem(m_pCanvasItem);
</code></pre>
<p dir="auto">}</p>
<p dir="auto">Dialog::~Dialog()<br />
{<br />
delete ui;<br />
}</p>
<p dir="auto">bool Dialog::eventFilter(QObject *pObject, QEvent *pEvent)<br />
{<br />
if (pObject == m_pView-&gt;viewport())<br />
{<br />
if (pEvent-&gt;type() == QEvent::Resize)<br />
{<br />
m_pScene-&gt;setSceneRect(m_pView-&gt;viewport()-&gt;rect());<br />
m_pCanvasItem-&gt;setRect(m_pView-&gt;viewport()-&gt;rect());<br />
m_pMyRectItem-&gt;setRect(m_pCanvasItem-&gt;rect());<br />
}<br />
}<br />
return QWidget::eventFilter(pObject, pEvent);<br />
}@</p>
<p dir="auto"><em>main.cpp</em><br />
@#include &lt;QtGui/QApplication&gt;<br />
#include "dialog.h"</p>
<p dir="auto">int main(int argc, char *argv[])<br />
{<br />
QApplication a(argc, argv);<br />
Dialog w;<br />
w.show();</p>
<pre><code>return a.exec(&amp;#41;;
</code></pre>
<p dir="auto">}@</p>
]]></description><link>https://forum.qt.io/topic/16243/qglwidget-issue</link><generator>RSS for Node</generator><lastBuildDate>Sun, 20 Sep 2026 14:06:37 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/16243.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 27 Apr 2012 07:23:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QGLWidget issue?! on Fri, 14 Nov 2014 10:25:17 GMT]]></title><description><![CDATA[<p dir="auto">hi,<br />
I have the exact same problem at the moment...<br />
In our application we also draw a dashed rectangle and when you zoom in in such a way that the viewport is inside the dashed rectangle, other objects start behaving very weird:</p>
<ul>
<li>the color changes like in the previous post</li>
<li>sometimes the solid-line objects inside the dashed rectangle are drawn on the same location if you pan the solid-lined object out of the viewport (so they don't disappear)</li>
</ul>
<p dir="auto">These problems are solved if we just don't use dashed lines but solid lines instead<br />
We are using Qt 4.8.6</p>
<p dir="auto">Have you ever found a solution for this? Or do you know if this issue has been solved in newer releases of Qt?</p>
<p dir="auto">Thanks in advance</p>
]]></description><link>https://forum.qt.io/post/151740</link><guid isPermaLink="true">https://forum.qt.io/post/151740</guid><dc:creator><![CDATA[dabu]]></dc:creator><pubDate>Fri, 14 Nov 2014 10:25:17 GMT</pubDate></item><item><title><![CDATA[Reply to QGLWidget issue?! on Fri, 27 Apr 2012 09:33:54 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">Ah, you could have mentioned that a bit earlier :)</p>
</blockquote>
<p dir="auto">Sorry!</p>
<p dir="auto">A similar problem occurs in the following simple application:<br />
@<br />
#include &lt;QtGui/QApplication&gt;<br />
#include &lt;QGLWidget&gt;<br />
#include &lt;QGraphicsView&gt;<br />
#include &lt;QGraphicsScene&gt;<br />
#include &lt;QGraphicsRectItem&gt;<br />
#include &lt;QWheelEvent&gt;<br />
#include &lt;qmath.h&gt;</p>
<p dir="auto">class View : public QGraphicsView<br />
{<br />
public:<br />
View()<br />
{<br />
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));</p>
<pre><code>    QGraphicsScene *pScene = new QGraphicsScene(this);
    setScene(pScene);
    pScene-&gt;setSceneRect(0, 0, 200, 200);

    QGraphicsRectItem *pItemSolid = new QGraphicsRectItem;
    pItemSolid-&gt;setPen(QColor(Qt::green));
    pItemSolid-&gt;setRect(70, 70, 60, 60);
    pScene-&gt;addItem(pItemSolid);

    QGraphicsRectItem *pItemDash = new QGraphicsRectItem;
    QPen oPen(Qt::DashLine);
    oPen.setColor(Qt::red);
    pItemDash-&gt;setPen(oPen);
    pItemDash-&gt;setRect(20, 20, 160, 160);
    pScene-&gt;addItem(pItemDash);
}
</code></pre>
<p dir="auto">protected:<br />
void wheelEvent(QWheelEvent *event)<br />
{<br />
qreal factor = qPow(1.2, event-&gt;delta() / 240.0);<br />
scale(factor, factor);<br />
event-&gt;accept();<br />
}<br />
};</p>
<p dir="auto">int main(int argc, char *argv[])<br />
{<br />
QApplication a(argc, argv);<br />
View v;<br />
v.show();<br />
return a.exec();<br />
}<br />
@</p>
<p dir="auto">When zooming in with the mouse wheel so that the red dashed <em>QGraphicsRectItem</em> gets outside of the view the solid green <em>QGraphicsRectItem</em> will become red:</p>
<p dir="auto">!<a href="http://www.bilderload.com/daten/1VNSJ1.png" target="_blank" rel="noopener noreferrer nofollow ugc">http://www.bilderload.com/daten/1VNSJ1.png</a>! !<a href="http://www.bilderload.com/daten/25ZCZ7.png" target="_blank" rel="noopener noreferrer nofollow ugc">http://www.bilderload.com/daten/25ZCZ7.png</a>!</p>
<p dir="auto">This only occurs when using <em>QGLWidget</em> and a <em>dashed pen</em>!</p>
]]></description><link>https://forum.qt.io/post/137224</link><guid isPermaLink="true">https://forum.qt.io/post/137224</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 27 Apr 2012 09:33:54 GMT</pubDate></item><item><title><![CDATA[Reply to QGLWidget issue?! on Fri, 27 Apr 2012 09:14:56 GMT]]></title><description><![CDATA[<p dir="auto">Ah, you could have mentioned that a bit earlier :)</p>
]]></description><link>https://forum.qt.io/post/137221</link><guid isPermaLink="true">https://forum.qt.io/post/137221</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 27 Apr 2012 09:14:56 GMT</pubDate></item><item><title><![CDATA[Reply to QGLWidget issue?! on Fri, 27 Apr 2012 09:11:36 GMT]]></title><description><![CDATA[<p dir="auto">When you use the mouse wheel to resize the <em>MyRectItem</em> (which paints a red rectangle and a green ellipse) and the red rectangle gets outside of the view it will change to a second red ellipse! But I DON'T draw any RED ellipse!</p>
]]></description><link>https://forum.qt.io/post/137219</link><guid isPermaLink="true">https://forum.qt.io/post/137219</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 27 Apr 2012 09:11:36 GMT</pubDate></item><item><title><![CDATA[Reply to QGLWidget issue?! on Fri, 27 Apr 2012 08:39:25 GMT]]></title><description><![CDATA[<p dir="auto">so what is the question?</p>
]]></description><link>https://forum.qt.io/post/137212</link><guid isPermaLink="true">https://forum.qt.io/post/137212</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 27 Apr 2012 08:39:25 GMT</pubDate></item><item><title><![CDATA[Reply to QGLWidget issue?! on Fri, 27 Apr 2012 08:35:46 GMT]]></title><description><![CDATA[<p dir="auto">I'm sorry, but I can't seem to notice any problem with that output... If you mean that the curved lines look a bit awkward, blame your monitor for having large pixels ;) or turn antialiasing on.</p>
]]></description><link>https://forum.qt.io/post/137209</link><guid isPermaLink="true">https://forum.qt.io/post/137209</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 27 Apr 2012 08:35:46 GMT</pubDate></item></channel></rss>