[Qt-interest] Working with QGradient

Sean Harmer sean.harmer at maps-technology.com
Fri Jun 12 10:04:53 CEST 2009


On Friday 12 Jun 2009 05:53:48 Santhosh Y wrote:
> Hi,
>
> Let us say I have a rectangle/polygon (0,0, 100,100) .
> I would like to set half portion of rectangle with One color and the
> other half with one more color.
>
> How can I achieve this?
>  +-----------------------------+
>
>   |                  Qt::lightGray        |
>
>  +-----------------------------+
>
>   |                 Qt::darkGray        |
>
>  +-----------------------------+
>
> I have overwritten the paintEvent (QPaintEvent*) of the above widget and
> I would like to set the
> QGradient  to the  QPainter.
Why use a gradient for only two colours? You can simply do something like this 
in your paint event:

QBrush b1( Qt::lightGray );
painter->setBrush( b1 );
painter->drawRect( 0, 0, 100, 50 );

QBrush b2( Qt::darkGray );
painter->setBrush( b2 );
painter->drawRect( 0, 50, 100, 100 );

But obviously it would be better if you used rects calculated from the widget 
geometry rather than hard-wired like this.

Sean




More information about the Qt-interest-old mailing list