C++ Help Needed

Feb 26, 2009 14:46

There are times I find C++ to be a deeply annoying language.

I have a class, lets call it A, and within that class definition I have the following:

private:
static QPainterPath *path;Which is to say, I'm declaring a static (class-wide) pointer to a path object; all instances of this class will now have the same shared data member. The path object ( Read more... )

Leave a comment

Comments 4

necaris February 26 2009, 16:04:56 UTC
I've just had a thought: you could define a method paintStuff(QPainterPath *pth) in the base class, which does all the stuff, and then a virtual method that gets the path pointer and calls paintStuff() with it?

Reply

tyr_arcana February 26 2009, 16:10:39 UTC
that's pretty much what it does anyway, the paint method is only two lines long.

void A::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
painter->setPen(*this->pen);
painter->drawPath(*this->path);
}

And yes, I could duplicate that in every subclass I write, and duplicate the similarly small functions to return the shape and bounding rect of the item, but then there would be very little need for the base class and I might as well just copy/paste the whole class each time I want a varient on it instead of subclassing.

Reply

necaris February 26 2009, 18:52:34 UTC
How about:
QPainterPath* A::getPath()
{
return A::path;
}

And the same little stub functions for your base classes, and then change your paint and bounding rect and all those functions to use getPath() to get hold of the path?

Reply


tyr_arcana February 27 2009, 14:11:29 UTC
For anyone curious, I solved this eventually with a little help from Stack Overflow (thanks necaris for suggesting it).

The thread can be found here.

Reply


Leave a comment

Up