Russian Qt Forum

Qt => Вопросы новичков => Тема начата: widoki от Июня 16, 2011, 00:21



Название: перемещение QGraphicsPixmapItem по сцене
Отправлено: widoki от Июня 16, 2011, 00:21
имеется класс
Код:
#ifndef CBOX_H
#define CBOX_H

#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QTimer>
#include <QPixmap>
#include <QPainter>

class myPic : public QObject, public QGraphicsPixmapItem
{
    Q_OBJECT
public:
    myPic( QGraphicsScene*);
    void myPicFallDown ();
    ~myPic();

    qreal GetX();
    qreal GetY();
    void setX(qreal);
    void setY(qreal);
   
protected slots:
    virtual void timerEvent(QTimerEvent *);

protected:
    QGraphicsPixmapItem *myPic;
    qreal x;
    qreal y;
};

#endif // CBOX_H

Код:
#include "mypic.h"

myPic:: myPic( QGraphicsScene* scene )
{
        x=1100.0;
        y=0.0;
        setOffset (x,y);
        myPic->setPixmap( QPixmap( ":/myPic/nameofmypic.PNG" ) );
        scene->addItem( this );
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(myPicFallDown()));
        timer->start(1000);   
}

void myPic::myPicFallDown()
{
        setY(GetY+5);
        setOffset (x,y);
}

нужно, чтобы картинка упала и осталась лежать на полу, а не соскальзывала вниз.


Название: Re: перемещение QGraphicsPixmapItem по сцене
Отправлено: kambala от Июня 16, 2011, 00:32
отключить таймер когда достиглись нужные координаты?


Название: Re: перемещение QGraphicsPixmapItem по сцене
Отправлено: widoki от Июня 16, 2011, 00:58
Код:
#include "mypic.h"

myPic:: myPic( QGraphicsScene* scene )
{
        x=1100.0;
        y=0.0;
        setOffset (x,y);
        myPic->setPixmap( QPixmap( ":/myPic/nameofmypic.PNG" ) );
        scene->addItem( this );
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(timerEvent (QTimerEvent *)));
        timer->start(1000);   
        if (GetY<=120) timer->stop();
}

void myPic::timerEvent (QTimerEvent *)
{
   myPicFallDown();
}

void myPic::myPicFallDown()
{
        setY(GetY+5);
        setOffset (x,y);
}


Название: Re: перемещение QGraphicsPixmapItem по сцене
Отправлено: widoki от Июня 16, 2011, 00:59
отключить таймер когда достиглись нужные координаты?

м?
if (GetY<=120) timer->stop();


Название: Re: перемещение QGraphicsPixmapItem по сцене
Отправлено: kambala от Июня 16, 2011, 01:45
так не в конструкторе ж (он вызывается только при создании айтема, ну по крайней мере так должно быть), а в myPicFallDown() или timerEvent()

да, и GetY же у тебя метод, а не переменная, потому GetY()