GtkDrawingArea - how to make it drawable?

后端 未结 2 623
情话喂你
情话喂你 2021-01-14 02:50

I\'m going out of my mind a bit here.

I\'m trying to draw some simple graphics on my GTK form using cairo.

#include 
#include 

        
2条回答
  •  悲&欢浪女
    2021-01-14 03:38

    The warning and the consequent crash in your first code is because of the fact that darea->window is NULL i.e. GdkWindow (darea->window) has not been created yet at the point in the program when you call gdk_cairo_create. GdkWindow for the drawing area is created once the drawing area widget is realized. Try adding gtk_widget_realize(darea); before the call gdk_cairo_create . Also instead of directly accessing window, may I suggest use of the accessor function gtk_widget_get_window.
    The reason why it did not assert & crash when you have added the same code inexpose-event callback is because GdkWindow associated with drawing area is already created at that point in the execution of the program.

    Edit:
    The reason provided above is only to explain why the code was not crashing in the expose-event callback & why the call gdk_cairo_create did not assert when the program was run. Treat the above response just as an explaination related to crash. Please refer ptomato's response for details regarding drawing mechanism

    Hope this helps!

提交回复
热议问题