PDA

View Full Version : Howto draw stuff



Morea
11th February 2006, 10:39
How should one draw stuff?
There are a few different things that can be done,
1)Draw lines, polygons, circles etc.
2)Draw letters
3)Draw something (lines, pixels, etc.) and then redraw it at a sligthliy different position (i.e. animation).
4)Load an image from a file and draw it, and possible also move it around as some kind of animation.

I read about the Arthur painting system and look at the tutorials but I can't make a connection between these two. How should these things REALLY be done?
Any ideas?

wysota
11th February 2006, 11:17
What exactly do you want to do?
Here are some painting examples (http://doc.trolltech.com/4.1/examples.html#painting-examples)

Morea
11th February 2006, 12:43
What exactly do you want to do?
Here are some painting examples (http://doc.trolltech.com/4.1/examples.html#painting-examples)
I have a lot of ideas, a simple game where you fly around in a space craft? A plotting tool (i.e. a lot of datapoints), etc.
I'm just interested in good strategies for drawingstuff.

wysota
11th February 2006, 16:02
Take a look at QPainter class and its superclass.

And remember to paint only in paintEvents. The examples mentioned above will give you a good start.

Morea
13th February 2006, 19:27
If I make a program where I can plot things, and then wish to select the things in the graph, by clicking them, how do I do this? I'm totaly clueless on these things.

wysota
13th February 2006, 20:07
Store a list of your objects and in the mousePressEvent of your widget check if you hit one of the items based on the coords of the click and stored items.

high_flyer
14th February 2006, 10:21
I have a lot of ideas, a simple game where you fly around in a space craft? A plotting tool (i.e. a lot of datapoints), etc.
I'm just interested in good strategies for drawingstuff.
3D and 2D plotting are very different.
For a 3D space flight I would use open GL or other external libs that do 3D rendering.
For 2D plotting Qt comes very well equiped, you should read the docs on painting and QPainter.

Morea
14th February 2006, 18:28
QCanvas, that was Qt3, right?

wysota
14th February 2006, 20:38
QCanvas, that was Qt3, right?

Yes. It is not needed in Qt4 anymore. All the magic features it had are now present in QWidget.

Morea
17th March 2006, 23:26
If I create a class for drawing

class Render:public QWidget
{
Q_OBJECT
public:
Render(QWidget* parent=0);
};

how can I set the size and position of the area where I should be doing the drawings if I'm not using the layout classes? Or should I really start using the layout tools?

jacek
17th March 2006, 23:40
how can I set the size and position of the area where I should be doing the drawings if I'm not using the layout classes?
QWidget::setGeometry()


Or should I really start using the layout tools?
Yes, you should. Don't forget to reimplement QWidget::sizeHint().

Morea
18th March 2006, 12:18
I'm having some problem understanding how stuff is drawn on a widget with the QPainter.
If I do


void Render::paintEvent(QPaintEvent* event)
{
int i;
QPainter painter(this);
for (i=0;i<10;i++)
{

painter.drawLine(QLine(rand()%250,rand()%100,rand( )%250,rand()%100));
}
}

Where Render is a only derived from QWidget and it is put in a Layout to stretch it out and make it big. The code works fine.
I make new lines by clicking a button that calls a slot in Render class that then runs

update();
to generate a QPaintEvent

I get 10 new lines every time, is it possible to keep the old lines and just draw new lines on the old ones?

munna
18th March 2006, 13:14
I get 10 new lines every time, is it possible to keep the old lines and just draw new lines on the old ones?

set Qt::WA_NoSystemBackground flag of QWidget.

Morea
7th April 2006, 11:39
But that gives me an ugly black backgroud, can I get a white background?

munna
7th April 2006, 12:09
When the flag Qt::WA_NoSystemBackground is set the background is not automatically repainted and that is why you get black background. Therefore once you set this flag you have to take care paiting even the background. Also, double buffering is disabled once you set this flag.

On the other hand, if you dont want to use this flag, you can draw your older lines and the new lines every time the paintEvent is called. I think this will be pretty fast and you will not find any kind of flicker or redrawing that is actually taking place.

Morea
7th April 2006, 12:32
But what if that means drawing tens of thounds random points? I will really have to store them all then? I'll try that then.

munna
7th April 2006, 13:05
You can either

paint background only once and also keep track of newly shown regions and paint their background also only once. Here you do not have to store your points and just keep drawing the new points.

or

Store all your points or line ...and then for every paintEvent redraw them. Here you do not have to keep track of the newly show regions which need to be painted white for the first time.