Frage

I've got a little GTK-Application with two buttons (A, B) and a drawing area (C). When I click on A, C's size should be recalculated and set with gtk_widget_set_size_request(). This will cause an expose-event, whose handler will calculate some data. Further, the click on A will emit a clicked signal at B, whose handler needs the data calculated by C's expose-event.

In fact everything works fine, but the expose-event delays somehow, and the clicked handler is called before and the data (you may guess it) is missing.

a_handler() {
   new_size = calculate_size();
   gtk_widget_set_size_request(C, new_size); //Will cause expose-event
   g_signal_emit_by_name(B, "clicked");
}

b_handler() {
   calculate_something(data); //Error
}

c_handler() {
   draw_something();
   data = calcluate_data();
}

What can I do?

War es hilfreich?

Lösung

The ugly fix is typically to add calls to gtk_main_iteration(), for instance between the call to gtk_widget_set_size_request() and the signal-emission.

Andere Tipps

Gtk (and most other GUI toolkits) does the painting and event handling all in one thread. Therefore when the expose event is emitted, it goes into a queue and will be handled only after a_handler returns.

The best approach in my opinion would be to move the calculation somewhere else, if there is not some good reason to tie it into GUI drawing. For example, there could be a separate method calculate_data that is called both from b_handler and c_handler.

The next best way is to call gdk_window_process_updates to force the expose event to be handled immediately: http://developer.gnome.org/gdk/stable/gdk-Windows.html#gdk-window-process-updates

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top