[Qt-interest] QPainterPath::contains() alternative behaviour

Ioannis Nousias s0238762 at sms.ed.ac.uk
Sat Apr 4 19:30:24 CEST 2009


Ioannis Nousias wrote:
> Jonathan Kew wrote:
>> On 4 Apr 2009, at 14:52, Ioannis Nousias wrote:
>>
>>> Hello Qt users/developers,
>>>
>>> I'm new to Qt and currently exploring its features, before I can use it
>>> for a project of mine.
>>>
>>> Before I ask my question, let me give you a sort description of my test
>>> case. I've made my own version of the 'scribble' example provided by 
>>> the
>>> Qt demos package. In this version, each shape is stored as a
>>> QPainterPath (along with an associated QPen, wrapped in what I call a
>>> pathContext). A new path is created on mousePressEvent. The paintEvent
>>> goes through the list of paths and strokePath() them on the widget's
>>> drawing area, using their companion QPen.
>>>
>>> In the mouseMoveEvent (I've set mouse tracking on), I iterate through
>>> the list of paths and using QPainterPath::contains(), I detect if the
>>> current mouse pointer is over a given path, in which case I change the
>>> QPen colour for that path to 'Qt::red', otherwise it's set to
>>> 'Qt::blue'. The end effect is, when the mouse pointer hovers over a
>>> path, the path gets highlighted red.
>>>
>>> Now imagine a U shaped path. The problem I have is that 'contains()'
>>> returns a positive response even when the mouse pointer is over the
>>> inner cavity of the U shape, as if the path is closed and filled. The
>>> required behaviour is for 'contains()' to return a positive response
>>> only if the mouse is over the stroke line of the path-shape.
>>>
>>> Is there a way to do this?
>>
>> Perhaps use QPainterPathStroker to create a path representing the 
>> outline of the stroked shape you're interested in, and test 
>> contains() on that?
>>
>> I haven't tried this, it just seems plausible.
>>
>> JK
>>
>>
>>
>>
thanks for the response.

yes, I've tried that, but contains() didn't trigger at all. Just to make 
sure I'm not doing something wrong, here is what I did:

QPainterPathStroker stroker;
QPainterPath stroke = stroker.createStroke(path);

bool contains = stroke.contains
(
QRectF(event->pos(),event->pos())
.adjusted(-0.5,-0.5,0.5,0.5)
);


Where 'event' is the QMouseEvent. Using the same 'contains' expression 
on the 'path' exhibits the behaviour I described in the previous e-mail, 
but in this version, 'contains' is never set to true.

I've plaid with the fill rules as well, but no luck.

any more ideas?

Ioannis


>


-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.




More information about the Qt-interest-old mailing list