logo       

Re: Why isn't this easy? implementing File->New: msg#00007

Subject: Re: Why isn't this easy? implementing File->New
--- darrik <darrik@xxxxxxxxxxxxxxxxxxx> 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 


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
linux.arklinux....    user-groups.lin...    kde.usability/2...    ietf.ipp/2002-0...    mail.spam.spamc...    os.netbsd.devel...    audio.cd-record...    text.unicode.de...    php.documentati...    games.fps.halfl...    window-managers...    suse.oracle.gen...    bug-tracking.gn...    video.dvdrip.us...    xfree86.cvs/200...    java.netbeans.m...    network.argus/2...    culture.sf.kill...    debian.ports.al...    freebsd.questio...    qplus.devel/200...    handhelds.palm....   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe