[Qt-interest] QBrush with custom CrossPattern

Samuel Rødal sroedal at trolltech.com
Fri Jun 12 10:18:17 CEST 2009


Iurii Gordiienko wrote:
> 
> Hi
> 
> I use the QGraphicsScene (and QGraphicsItem) for drawing chart-like 
> widget for showing some graph information. 
> But in result I have lines from Qt::CrossPattern style above of the my 
> own image (see attached screenshot)...
> I need black background with green lines under my image...
> This is part from my code for reproduce the current behavior:
> class GraphicsView : public QGraphicsView
> GraphicsView::GraphicsView()
> {
>   scene()->setBackgroundBrush(QBrush(QColor(0,0,0)));
>   scene()->setForegroundBrush(QBrush(QColor(0,124,0), Qt::CrossPattern));
> }
> class GraphicsItem : public QGraphicsItem...
> void GraphicsItem::paint(QPainter* painter, const 
> QStyleOptionGraphicsItem* option,QWidget*)
> {
>   static QPen pen(QBrush(QColor(0,252,0),Qt::SolidPattern),2);
>   
>   painter->setPen(pen);
>   painter->draw(...);
> }
> 
> 
> Qt-4.5.1
> Thanks

The foreground brush will be drawn in the end and thus on top of all the 
graphics items. To get a cross pattern with a black background you 
should probably set a pixmap brush on the background instead, like this:

QPixmap pix(8, 8);
pix.fill(Qt::black);

QPainter painter(&pix);
painter.fillRect(pix.rect(), QBrush(QColor(0, 124, 0), Qt::CrossPattern));
painter.end();

scene()->setBackgroundBrush(pix);

Regards,

Samuel



More information about the Qt-interest-old mailing list