Re: destroying widgets and Gtk2::GladeXML



On Sat, 2003-08-02 at 22:31, James Curbo wrote:
Hi, I'm a semi-newbie at GTK+ programming, and I have a question. (love
the work on gtk2-perl, by the way; exciting stuff, especially
SimpleList)

Say I have created an app using Glade, and have a Perl program that uses
it in the normal way. In this Glade project I have a main window and a
dialog that pops up when you press a toolbar button. The way I used to
do this in gtk-perl and in C was:

in the 'clicked' callback for the button to create a new GtkDialog like
so in gtk-perl:

my $dialog = new Gtk::Dialog();
$dialog->set_title("New Dialog");
# create ok button, connect signals etc
$dialog->show_all;

then in the 'clicked' handler of the OK button of the dialog, do
something like $dialog->destroy().

I find that if I do this with widgets I get from Glade via get_widget()
in gtk2, I can not use get_widget to get them again if I call destroy().
Does this mean that the Glade module initializes all windows at the
beginning? I got around the problem using $dialog->hide but I am
wondering if this is the Right Thing to do or if I am missing a more
obvious or better solution.

Thanks!

the following code:
##############################################

use Gtk2 '-init';
use Gtk2::GladeXML;

$gld = Gtk2::GladeXML->new('glade_test.pl.glade');

$gld->signal_autoconnect_from_package('main');

$btn = $gld->get_widget("button1");

sub on_button1_clicked
{
        my $dialog = new Gtk2::Dialog;
        $dialog->set_title("New Dialog");
        # create ok button, connect signals etc
        $dialog->run;

        $dialog->destroy;
}

Gtk2->main;
##############################################

seems to behave as would be expected. i created a trivial glade-2
project with 1 button and added a clicked callback. 

it also seems to work when i remove the signal handler from the glade-2
project and grab the button myself and connect the callback

$btn = $gld->get_widget("button1");
$btn->signal_connect( clicked => \&on_button1_clicked );

i click the button as many times as i wish and it seems to pop up the
dialog and it goes away at the destroy, the next time around it's all
new and works as expected

so the way you used to do it works, but as for the glade stuff my best
explanation is that it creates the widget when new is called. it then
destroys the widget when you call destroy. destroy does just that after
called it will no longer exist so the next time you try use it nothing
is there to use. it's somewhat of a problem with glade and the way
dialogs work. hide is probably the best workaround, but won't suffice in
all situations, mainly when you would want to have a completely new
dialog without the changes made last time. 

another possible workaround, but not ideal is to define the dialog in a
separate glade file and in the button clicked handler create a new glade
object that is a dialog. this has lots of issues though, but could be
done. it would only be worth it if you had a really fancy complicated
dialog that is way to hard to create by hand, well that you don't want
to anyway. 

sorry, probably left you with more questions than you started, but
hopefully it helped a little. i guess if nothing else, hide is about the
best way to deal with the less than perfect situation.

personally i would suggest using glade for prototyping apps, and then
reimplementing them the long way if you're going to be using them in any
kind of real environment. i think you need the control of the long way
if you're going to be trying to get all of the bugs and issues out of a
real app, but that's just me. 

-rm




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