LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   GTK in C - How to update a label from within a function (https://www.linuxquestions.org/questions/programming-9/gtk-in-c-how-to-update-a-label-from-within-a-function-4175636237/)

j4g 08-12-2018 03:26 PM

GTK in C - How to update a label from within a function
 
I'm struggling with something that seems trivially simple (please don't laugh).

I have a button whose callback function looks like this:

Code:

void function() {
    gtk_label_set_text(label, "Hello");

    /*
    *
    * Other stuff
    *
    */
}

I expect this function change the label's text to "Hello" and then perform "other stuff", in that order. However, the label doesn't update until the whole function finishes.

How can I update the label (or perhaps "refresh" the window that the label is a part of) before "other stuff" happens?

Keith Hedger 08-12-2018 06:56 PM

https://developer.gnome.org/gtk3/sta...tk-widget-show

j4g 08-13-2018 07:35 AM

Thank you for your reply. Unfortunately, calling gtk_widget_show() doesn't seem to make a difference, at least not in my case.

I'm thinking that although functions like gtk_label_set_text() and gtk_widget_show() modify some internal values of the widgets, the actual redrawing (from the user's perspective) doesn't happen until the callback function terminates.

I thought I could get around this by assigning two callback functions to the same widget, one for handling the label and one for handling the other stuff, but unfortunately it had the same result (the widget didn't get redrawn until both functions terminated).

Just to be clear, my desired sequence is:

1. User interacts with widget (clicks a button in this case)
2. Label changes
3. Other stuff happens

It seems that no matter what I do, 2 and 3 always happen out of order from the user's perspective... :cry:

Keith Hedger 08-13-2018 07:59 AM

try
Code:

while(gtk_events_pending())
        gtk_main_iteration();

After setting the label.

j4g 08-15-2018 08:35 AM

Yes, that did the trick. :)


All times are GMT -5. The time now is 12:19 AM.