PDA

View Full Version : QBrush pattern problem



ilovethisgame
7th July 2006, 07:16
When window size is changed by using QPainter:: setWindow(...)
during a zooming process, the behaviour of brush pattern is very
wierd (please look at the attached bmp). Is there any way to make
the brush pattern stable?

//Painting codes:

void paint_test:: paintEvent(QPaintEvent * e)
{
QPainter painter(this);
painter.setPen(Qt::red);

QBrush brush(Qt:: Dense6Pattern);
brush.setColor(Qt::blue);
painter.setBrush(brush);

painter.setWindow(0, 0, 1000, 1000);
painter.drawRect(50, 50, 100, 200);

painter.setWindow(0, 0, 2000, 2000);
painter.drawRect(500, 100, 200, 400);

painter.setWindow(0, 0, 3000, 3000);
painter.drawRect(1350, 150, 300, 600);

painter.setWindow(0, 0, 4000, 4000);
painter.drawRect(2600, 200, 400, 800);

painter.setWindow(0, 0, 5000, 5000);
painter.drawRect(4250, 250, 500, 1000);
}

jacek
9th July 2006, 17:49
Indeed, it behaves strange. Especially when you resize the widget. It might be a result of some round-off errors, but IMO you should show this example to the Trolls.

Below is a working example:

#include <QApplication>
#include <QPainter>
#include <QWidget>

class Test : public QWidget
{
protected:
void paintEvent( QPaintEvent * )
{
QPainter painter(this);
painter.setPen(Qt::red);

QBrush brush(Qt:: Dense6Pattern);
brush.setColor(Qt::blue);
painter.setBrush(brush);

painter.setWindow(0, 0, 1000, 1000); // try to comment out this line
painter.drawRect(50, 50, 100, 200);

painter.setWindow(0, 0, 2000, 2000);
painter.drawRect(500, 100, 200, 400);

painter.setWindow(0, 0, 3000, 3000);
painter.drawRect(1350, 150, 300, 600);

painter.setWindow(0, 0, 4000, 4000);
painter.drawRect(2600, 200, 400, 800);

painter.setWindow(0, 0, 5000, 5000);
painter.drawRect(4250, 250, 500, 1000);
}
};

int main( int argc, char **argv )
{
QApplication app( argc, argv );

Test t;
t.show();

return app.exec();
}

jneuhaus20
4th October 2012, 22:07
6 years later... I ran into this same problem, and only found this post referring to it. I solved it by doing the following:



painter.setWindow(0,0,_logicalWidth,_logicalHeight ); //setting the logical window

QBrush brush(Qt::blue,Qt::Dense6Pattern);

//add a transform to the brush to undo the logical scaling
brush.setTransform( QTransform::fromScale( width()/_logicalWidth, height()/_logicalHeight) );

painter.setBrush(brush);

//and carry on drawing


Hopefully this helps anyone who stumbles across it!