how to apply Design Pattern [Command] in Qt/KDE?

Chris Ryan xgbe at yahoo.com
Tue Feb 26 13:43:33 GMT 2002


--- Dung Patrick <dkt at digitalme.com> wrote:
<< snip >>
> 
> Both books talked about many useful design pattern.
> But I want to apply the design pattern in Qt
> (Command Pattern) but cannot get any hints. For
> Java, it has a ActionLisntener to call the execute
> method. How to do this with the signal and slot in
> Qt?

    I've been working with Qt for several months now
and Java for more than a year.  From my perspective
the Signal/Slot solution in Qt is _very_ similar in
result to the event model java has with the
ActionListener interface.  The biggest difference is
that in Qt you can specify a specific method to be the
target rather than just the actionPerformed() method
in Java.

    If you look at the below code the end result is
equivalent:

---- Java ----
class Bar {
    // whatever data is needed to store the Listener
    addActionListener(ActionListener al) {
        // do whatever is done to handle the Listener
    }
}
class Foo implements ActionListener {
    Foo() {
        Bar b = new Bar();
        b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        doSomething();
    }
    void doSomething() {
        // process the event
    }
}
---- Qt ----
class Bar : public QObject {
    Q_OBJECT
    signals:
        void eventPerformed();
};
class Foo : public QObject {
    Q_OBJECT
    public:
        Foo() {
            Bar * b = new Bar();
            // this connect has the same result as
            // the addActionListener does in the
            // above Java example.
            connect(b   , SIGNAL( eventPerformed() ),
                    this, SLOT  ( doSomething()    )
);
        }
    public slots:
        void doSomething() {
            // process the event
        }
};
---- end examples ----

    I hope this helps.

Chris Ryan


__________________________________________________
Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games
http://sports.yahoo.com

-
to unsubscribe from this list send an email to kdevelop-request at kdevelop.org with the following body:
unsubscribe »your-email-address«



More information about the KDevelop mailing list