PDA

View Full Version : QGraphicsItem filling with pattern



fvila
8th November 2010, 16:42
Hello,

I'd like to fill a QGraphicsItem with a pattern specified by a bitmap. Just like Qt::SolidPattern, Qt::Dense1Pattern, but a custom defined one.

For now I've tried drawing a QBitmap for the pattern with only black and white, and appliy this to a QBrush with a color. But the pattern doesnt draw correctly on the screen, and also I get this error:

QBrush: Incorrect use of TexturePattern

On MacOS the items are not filled, and in Windows and Linux the filling is not correct and it breaks when zooming, for example.

The code I used to create the brush is the following:



QBitmap *texture = createBrushFromPacket(layerPacket);
QColor fillColor = QColor::fromRgb(r, g, b);

QBrush fillBrush(fillColor, Qt::TexturePattern);
fillBrush.setTexture(*texture);
fillBrush.setColor(fillColor);


createBrushFromPacket returns the bitmap, and r, g, b contain the RGB color values.

Later on my code, when I draw the QGraphicsItem I do the following:



QGraphicsEllipseItem *item = new QGraphicsEllipseItem(rx, ry, radius, radius);

item->setBrush(fillBrush);
item->setPen(fillColor);


What am I doing wrong?

Thanks in advance,

Francesc Vila

fvila
24th November 2010, 15:21
I have resolved the "QBrush: Incorrect use of TexturePattern" error using:



QBrush fillBrush(fillColor, *texture);

and later, when adding an item:



QGraphicsEllipseItem *item = new QGraphicsEllipseItem(rx, ry, radius, radius);

item->setBrush(fillBrush);
item->setPen(fillColor);


But the pattern still does not show correctly. In fact, in MacOSX with Qt 4.7, the items are not filled.

Anyone knows the correct way to create filling patterns?

Regards,

Francesc