[Kde-perl] Why isn't this easy? implementing File->New

Ashley Winters jahqueel at yahoo.com
Wed Aug 2 05:51:53 CEST 2006


--- darrik <darrik at mythofbutterfly.com> wrote:
> 
> I've found this works, and I'm thinking it has to do with variables 
> going out of scope, automagic destructors, and perl garbage
> collecting.
> 
> sub processFileMenu {
> 	if ($option==FILENEW) {
> 		our $newwin->{this()}=MyView();
> 		$newwin->{this()}->show();
> 	}
> }

Yes, the garbage collector got him.

As a general policy, when you create widgets in Perl, you should treat
them the way C++ Qt does -- as children of your main window, with the
parent responsible for their ultimate destruction, if they don't close
themselves first.

The usual incantation should be:

package MyView;
use strict;
use Qt;
use Qt::isa qw(Qt::Widget);
sub NEW {
    my $class = shift;
    my $parent = shift;
    $class->SUPER::NEW($parent);

    # initialize yourself here
}

The garbage collector won't delete widgets which have a ->parent(), and
you should probably pass 'this' as the parent window.

use strict;
use Qt;     # use Qt in EVERY package! It declares 'this', and does
other use-strict-happy things
use MyView;
sub processFileMenu {
	if ($option==FILENEW) {
		MyView(this)->show;
	}
}

Cheers,
Ashley Winters

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Kde-perl mailing list