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

Ashley Winters jahqueel at yahoo.com
Wed Aug 2 08:12:18 CEST 2006


--- darrik <darrik at mythofbutterfly.com> wrote:

> Ashley Winters wrote:
> > 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;
> > 	}
> > }
> 
> You're not storing a perl reference to the new window anywhere.  What
> 
> happens when you want to access that window from the parent later?

Ahh, in that case you want to store it as a member variable. If, for
example, we wanted to populate the Window menu with the list of windows
opened with File->New or something...

package YourClass;
use strict;
use Qt;
use Qt::isa qw(Qt::Widget);
use Qt::attributes qw(windows);    # declare a member variable
use MyView;

sub NEW {
    # standard preamble

    windows = [];    # initialize as an array
}

sub processFileMenu {
	if ($option==FILENEW) {
		my $viewer = MyView(this);
		$viewer->show;
		push @{ windows }, $viewer;
	}
}

> 
> This is more evident when you subclass a container widget that has
> child 
> controls.  For instance:

I'll make the edits inline...

> 
> use strict;
> 
> package MyWidget;
> 
> use Qt;
> use Qt::isa qw( Qt::Widget );

use Qt::attributes qw( lbl );

> 
> sub NEW {
>    # irrelevant method arguments left out for brevity :P
> 
>    my $class=shift;
>    my $parent=shift;
>    shift->SUPER::NEW($parent);
>    my $layout=Qt::HBoxLayout(this);
#>    my $lbl=Qt::Label("label",this);
#>    $layout->addWidget($lbl);

    # instead
    lbl = Qt::Label("label",this);
    $layout->addWidget(lbl);
> }
> 
> sub changeChildLabel {
>    # how do you access $lbl here?
    lbl->setText("Qt::attributes");
> }


> This part has me confused.  Storing $lbl in a package variable
> doesn't 
> work if you instantiate several MyWidget's.  I've been using
> workarounds 
> that are inelegant, so a pointer to the *proper* way to do this would
> be 
> immensely appreciated.

Sure. Keep in mind that the 'this' function/variable/keyword thing is,
in fact, a hash. You're free to store things in it, like so:

this->{'lbl'} = Qt::Label(...);

In fact, that's all the Qt::attributes pragma does, is setup that
shortcut. It automatically creates a lbl() function which returns
this->{'lbl'}, in a way that lets you assign to it (as an lvalue).

That's the technical side, at least. For more of a tutorial, read this:

http://perlqt.sourceforge.net/dist/current/doc/en/index.html#using_attributes

- 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