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

Jonathan Kew jonathan_kew at sil.org
Sat Apr 4 17:04:12 CEST 2009


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





More information about the Qt-interest-old mailing list