PDA

View Full Version : cursor artifact while grabbing graphics item



totem
22nd December 2009, 20:57
Hi!

I make an application using QGraphicsItems (Qt 4.5.3), which basically allows the user to draw polygons in a scene/view


Now, when I grab a vertex, a graphic artifact appears under my cursor (see attached picture). I suspect this is due to either the vertex bounding rect (which is computed in order to be zoom-independant), or to the cursor itself; but i can't figure out what is happening.

If anyone has a few minutes to spend on my case, the project can be found here :
project home page on google code (http://code.google.com/p/ballelevgenerator/source/checkout)

A Windows executable can be found here :
Windows executable download (http://code.google.com/p/ballelevgenerator/downloads/list)


The artifact appears when i use the [Add Vertex] tool, not when i simply move a vertex with the [Select] tool :S

I'm sorry if i'm not clear enough, i could try to be more precise if you have questions

Thank you all

totem
30th December 2009, 16:35
no known bugs with cursor mask or apparented things (hoverevent conflicts, grabbed items weird behavior, etc.) ?

soxs060389
31st December 2009, 00:37
Make your svn code compile and I'll give it a try...

psih128
31st December 2009, 01:05
Make your svn code compile and I'll give it a try...
I second that - does not compile on mac.

As for artifacts... I had similar issues with my graphics app. In my app I created a few custom QGraphicsItems derived classes, which at first were giving me a lot of pain. The rule of thumb is not do paint outside of the region limited with the path returned by the QGraphicsItem::shape() function. While drawing QGraphicsScene/View does not bother to erase/redraw whatever was outside of that region. So if you do paint outside - all this stuff will most likely keep stacking up every time your item is redrawn, which will lead to a "leak of ink" in the form of artifacts.

Hope that helps.

totem
1st January 2010, 12:47
Make your svn code compile and I'll give it a try...

i forgot to precise i'm linking statically (so I don't have to make an installer). This might be the problem. File to change are TSPLib.pro and TSPViewer.pro




As for artifacts... I had similar issues with my graphics app. In my app I created a few custom QGraphicsItems derived classes, which at first were giving me a lot of pain. The rule of thumb is not do paint outside of the region limited with the path returned by the QGraphicsItem::shape() function. While drawing QGraphicsScene/View does not bother to erase/redraw whatever was outside of that region. So if you do paint outside - all this stuff will most likely keep stacking up every time your item is redrawn, which will lead to a "leak of ink" in the form of artifacts.

Hope that helps.

Thank you for feedback, i'll take a look at this :)

soxs060389
2nd January 2010, 17:01
I am comiling on Fedora / Linux, and I rather prefer dynamic linking, and if you want to share your code with dev's it's much easier to dynamically link it, makt it link statically for a release version. Just a thought.
Sincerely

totem
5th January 2010, 09:16
indeed you are absolutely right. i committed changes and compiled without any problem with QtCreator yesterday, so from now the shared-build version is available on svn.

Back to my problem, i considered what psih128 said, but unfortunately i can not see where the QGraphicsItems would be a problem.

Here is the code which determines my item's rect (full code here (http://code.google.com/p/ballelevgenerator/source/browse/trunk/TSPViewer/tspvertexitem.cpp)) :



void
TSPVertexItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
qreal lod = option->levelOfDetail ;
double radius = 2.0/lod ;

m_rect.setRect( -radius, -radius, 2*radius, 2*radius ) ; // <- HERE

painter->save();
painter->setRenderHint(QPainter::Antialiasing);

QPen _pen ;
_pen.setCosmetic(true) ;

// setColor on QPen ...

painter->setPen( _pen ) ;
painter->drawRect(m_rect) ;

painter->restore();
}

And here is my QGraphicsItem::shape() function


QPainterPath
TSPVertexItem::shape() const
{
QPainterPath path;
path.addRect( m_rect );
return path;
}

This said, the artifact seems to be size-constant in scene coordinates (i.e : if i zoom in, artifact is bigger and vice-versa). I don't use to insist like this, but here and now I'm stuck; any comment/idea will be gracefully accepted

wysota
5th January 2010, 10:29
Why do you change m_rect inside your paint() method? You shouldn't do that. If you want your item to have the same size regardless of the zoom factor, set the ItemIgnoresTransformations flag on your item instead.

totem
5th January 2010, 11:19
thank you for this comment wyzota (tried this once but somehow it bugged)

I applied your idea and it's better now, though not resolved : there are no more ink leak but there still remains a black rectangle under cursor when using the [Add vertex] tool... I'll explore other possibilities tonight :/

totem
5th January 2010, 13:02
Found it...

Problem came from the TSPEdgeItem class : when i don't set the painter antialising render hint, artifact disappears.
On one hand i'm happy because i found where bug comes from, on the other hand it's disapointing 'cause i don't really understand the workaround, plus i can't use Antialiasing to render this item...


thank you for your attention

wysota
5th January 2010, 13:11
You need to adjust boundingRect() of your item to compensate for anti-aliasing. Just increase the bounding rect (and shape) by 1 in each direction (meaning 0.5 from each side of the rectangle).

totem
9th January 2010, 12:11
i resolved the bug considering your answer : i detected where and when my edge-items had zero-length (destPoint==sourcePoint) - which was the cause of all this - and avoid it to be drawn
thank you