PDA

View Full Version : Trouble with rendering to the widget



dosto.walla
28th September 2008, 04:38
Hello forum,

I am trying to render and see how different gradient types work and started with the linear gradient one.

The folloowing code snippet only shows a blank window widget that i have declared.



QApplication app(argc,argv);

QWidget window;

QPainter painter;

QLinearGradient gradient(0,0,100,100);

gradient.setColorAt(0,Qt::red);
gradient.setColorAt(0.5,Qt::green);
gradient.setColorAt(0,Qt::blue);

painter.setBrush(gradient);


painter.drawRect(0,0,100,100);

window.show();


return app.exec();


Any idea what i missed?


Regards
Sajjad

hkvm
28th September 2008, 12:42
Hello,

It seems to me you created a painter that has nothing to do with the window, so it has no effect.

Anyway, what you should do if you want to handle painting of the widget yourself is subclass QWidget and reimplement its paintEvent, otherwise even if you managed to draw into the window, its own paintEvent would redraw it.

The "analog clock" example in Qt docs shows how to do it: http://doc.trolltech.com/4.2/widgets-analogclock.html

dosto.walla
29th September 2008, 14:09
Hello,

Thanks again for the hint!!

Now i have another issue.


I would like to have multiple viewport on the same device (may that be QWidget, QPixmap, QPicture , QImage or QPrinter). I am not sure though if that will be efficient way to do that .

I attached an image where you will see three different shapes drawn with three
different types of gradient at three different placements. Now if i change the size of the window those shapes do not increase or decrease by size proportionally.


then i thought that having multiple viewport will be one of the solutions.

Can you suggest anything better?



Regards
Sajjad

hkvm
29th September 2008, 16:14
To draw independently of the underlying device, you can simply do something like this:


QPainter painter(this);
painter.setWindow(QRect(0, 0, 100, 100));


Then you draw as if you had a window 100 x 100 pixels, and your drawing will be stretched to whatever size the window (or something else you're drawing to) actually has.

You can read more about this in the docs:
http://doc.trolltech.com/4.1/coordsys.html#window-viewport-conversion
It also says how to maintain the aspect-ratio for your drawing to prevent deformation when you resize the window so it's not square.

QbelcorT
29th September 2008, 16:44
Hi
I would suggest using QGraphicsScene, QGraphicsView, QGraphicsItem. These classes were made for mulitple viewports. Check the examples/graphicsview directory.