[Gambas-user] Why is this code running twice?

Adrien Prokopowicz adrien.prokopowicz at ...626...
Wed Aug 2 12:05:47 CEST 2017


Le Wed, 02 Aug 2017 03:38:33 +0200, Fernando Cabral  
<fernandojosecabral at ...626...> a écrit:

> I am trying to create a sundial faceplate. I took the analogic clock,
> discarded what I did not need and thought I was ready to work on the
> calculations and drawing of the hour angles.
>
> Nevertheless, I am mystified by two things I can't understand:
> a) The code produces a loop I can't see why (I am not referring to the  
> FOR
> loop)

By default, DrawingAreas are not cached, which means it will need to  
redraw its
contents a few times depending on the situation (most likely on startup,  
or when
the window is resized). The docs[0] talk about that a little bit.

If you do not want this to happen, you need to set the Cached[1] property  
to True,
which will make the DrawingArea store the image in memory instead of  
redrawing it
every time.
In practice, this means two things :

- Since the whole contents of the DrawingArea is stored, it will increase  
the memory
   usage a bit, however since nowadays every CPU and GPU has access to more  
than 1GB
   of memory, I would say it is negligible.
- If you want to actually update the drawing, you will have to manually  
call the
   Refresh() method[2].

> b) I can't modify the drawing area dimensions ( dwgArea.width = 800  does
> not work)

We would neet your full project to be sure, but most likely your  
DrawingArea is
inside a Form (or another container) that resizes its children, making  
fixed
coordinates useless.

If you want a fixed size window, you should try to resize the Form itself,
however I would recommend to let the user resize the window as they want,
and calculate the size of your drawing by reading the DrawingArea's
ClientWidth and ClientHeight properties.

However, depending on your drawing this may be a little tricky, so for  
small
experiments having a fixed-sized window is okay too. :-)

>
> Besides, I have a question: when a drawing area is created, it (0,0)
> supposed to be the central point?
>

No. Unlike math where the origin (0,0) is usually represented in the  
center,
the origin of screen coordinates is always located in the top-left corner.

If you want the actual center of your DrawingArea, you will have to use
the following coordinates :

x = DrawingArea.Width / 2
y = DrawingArea.Height / 2

Hope this helps. :-)

Regards,

[0] http://gambaswiki.org/wiki/comp/gb.qt4/drawingarea
[1] http://gambaswiki.org/wiki/comp/gb.qt4/drawingarea/cached
[2] http://gambaswiki.org/wiki/comp/gb.qt4/drawingarea/refresh

-- 
Adrien Prokopowicz




More information about the User mailing list