Re: What is the minimum number of lines to update a gui window without user clicking a button



Quoth Andrew Potter:
    void gui_print(const Glib::ustring& str) {
        /* Get a function pointer to the set_text method we want to
         * use. We must explicitly declare the method pointer
         * signature because Gtk::TextBuffer::set_text is an
         * overloaded method. */
        void (Gtk::TextBuffer::*fptr)(const Glib::ustring&) =
&Gtk::TextBuffer::set_text;

        /* Create a functor that points at the TextBuffer method we
         * want to use. Because tb is a Glib::RefPtr and sigc::mem_fun
         * needs the actual Gtk::TextBuffer object, we must use
         * operator->(). See GNOME Bugzilla #495762. */
        sigc::slot<void, const Glib::ustring&> set_text_functor =
sigc::mem_fun(tb.operator->(), fptr);

        /* Bind the arguments of the functor to create a
         * sigc::slot<void> that can be sent to the callback
         * dispatcher.
         */
        sigc::slot<void> bound_functor = sigc::bind(set_text_functor,
str);

        /* callback_dispatcher can execute any sigc::slot<void> on the
         * Main Loop. Since we have one now, send it.
         */
        callback_dispatcher.send(bound_functor);
    }

Given that sigc magic is hard for many people, you might want to simplify
this with a helper method:

    // Called from the blocking operation in the worker thread
    void gui_print(const Glib::ustring& str) {
        sigc::slot<void, const Glib::ustring&> print_functor =
sigc::mem_fun(this, &Example::gui_print_ui);
        sigc::slot<void> bound_functor = sigc::bind(print_functor, str);
        callback_dispatcher.send(bound_functor);
    }

    // Called on the UI thread when the worker thread calls gui_print
    void gui_print_ui(const Glib::ustring& str) {
        tb->set_text(str);
    }

This would make it easier for people to modify the actual action taken in
gui_print_ui.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]