[Qt-interest] Sleep

Tim W timpie.w at gmail.com
Wed Apr 8 11:39:23 CEST 2009


>"boost" location. Is that to be added a separate module? I am getting
"error" in that.

It's from the boost lib.  I used a lambda expression so the class can be
used with anything u want.

If you want to get rid of the boost function you can change it into a an
interface, something like this.

// header

#include <QObject>
#include <QSize>
#include <boost/function.hpp>

class QTimeLine;

class IResize : public QObject
{
public:
    void resize( int w, int h) { doResize( w, h ); }

protected:
    IResize( QObject * const parent ): QObject(parent) {}

private:
    virtual void doResize( int w, int h) = 0;
};

template<class T>
class WeResizeWrapper : public IResize
{
public:
    WeResizeWrapper( T& aObject, QObject * const parent ) : IResize(parent),
m_object(aObject ) {}

private:
    virtual void doResize( int w, int h) { m_object.resize(w,h); }
    T& m_object;
};


class ResizeAnimation : public QObject
{
    Q_OBJECT
public:
    typedef boost::function< void (int, int) > Resize;

    explicit ResizeAnimation( QObject* const aParent );
    void setResizeObject( IResize * const aResizableObject, const QSize&
aEndSize );

public slots:
    void start();

private slots:
    void nextFrame(int aFrame );

private:
    QTimeLine *const m_timeLine;
    QSize m_size;
    IResize *m_resizable;
};


/// cpp
#include "resizeanimation.h"
#include <QTimeLine>

ResizeAnimation::ResizeAnimation( QObject* const aParent )
: QObject(aParent),
  m_timeLine( new QTimeLine( 2000, this ) ),
  m_size(),
  m_resizable(0)
{
    connect( m_timeLine, SIGNAL( frameChanged( int ) ), SLOT(nextFrame(int))
);
    m_timeLine->setLoopCount( 5 );
}


void ResizeAnimation::setResizeObject( IResize *const aResizableObject,
const QSize& aEndSize )
{
    m_resizable =aResizableObject;
    m_size = aEndSize;
    m_timeLine->setFrameRange(0, qMax( aEndSize.width(), aEndSize.height() )
);
}

void ResizeAnimation::start()
{
    m_timeLine->start();
}

void ResizeAnimation::nextFrame(int aFrame )
{
    if(m_resizable)
    {
        m_resizable->resize(
            qMin( aFrame, m_size.width() ),
            qMin( aFrame, m_size.height() ));
    }
}

// client
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget widgetToResize;
    widgetToResize.resize( 0, 0 );
    widgetToResize.show();

    ResizeAnimation animation(0);
    animation.setResizeObject(
        new WeResizeWrapper<QWidget>( widgetToResize, &animation ),
        QSize( 500,500 ));
    animation.start();
    return a.exec();
}




Cheers,
Tim

--
An Apple a day keeps Windows away

ACCU - http://www.accu.org - Professionalism in Programming.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090408/7da20b9b/attachment.html 


More information about the Qt-interest-old mailing list