Results 1 to 3 of 3

Thread: Problem with rotating QGraphicsItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Location
    Kraków, Poland
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Problem with rotating QGraphicsItem

    I've wirriten a class which describes a piece for tetris-puzze.
    Each Piece shoud have a rotation method:

    class Piece : public QPixmap, public QGraphicsPixmapItem
    {
    public:
    void rotateLeft();
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QRectF boundingRect() const;

    private:
    QPixmap pixm;
    };

    Piece::Piece(QPixmap & pixmap)
    : QPixmap(pixmap),QGraphicsPixmapItem(pixmap)
    {
    pixm= pixmap;
    }


    void Piece::rotateLeft()
    {
    double pi = 3.14;
    double a = pi/180 * (-90.0);
    double sina =sin(a);
    double cosa =cos(a);

    QMatrix rotationMatrix(cosa,sina,-sina,cosa,0,0);
    QPixmap pixmap = this->transformed(rotationMatrix);
    pixm= pixmap;
    }

    void Piece:aint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    painter->drawPixmap ( 0,0, pixm );
    }

    QRectF Piece::boundingRect() const
    {
    return QRectF(0,0,pixm.width(),pixm.height());
    }

    When I would like rotate piece I use this code:


    QGraphicsScene *scene = new QGraphicsScene();
    QList<Piece*> list = model->getPieces();
    Piece * p = list.takeFirst();
    scene->addItem(p);
    p->rotateLeft();
    scene->update();
    p->rotateLeft();
    scene->update();

    When I run this program I shoud see a piece rotated twice in left direction, but I see rotated it only once. Why?
    Last edited by petrus; 22nd January 2007 at 02:55.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with rotating QGraphicsItem

    QPixmap pixmap = this->transformed(rotationMatrix);
    pixm= pixmap;
    This might be the problem.... u r transforming this but storing transformed pixmap in pixm...
    if u r using pixm for internal pixmap, apply the transformation on it...
    hope this helps

  3. #3
    Join Date
    Jan 2007
    Location
    Kraków, Poland
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: Problem with rotating QGraphicsItem

    I've sovled that by simplifing this class. Now inherits only QGraphicsPixmapItem and don't contain pixm field. If you are interested in I can present it, but now I'm to tired
    Thanks for your help.
    Last edited by petrus; 22nd January 2007 at 04:27.

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  2. Rotating QGraphicsItem
    By Gopala Krishna in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2006, 11:50
  3. QGraphicsItem problem - how to save items info ??
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2006, 12:17
  4. problem translating & rotating text
    By impeteperry in forum Qt Programming
    Replies: 9
    Last Post: 3rd July 2006, 19:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.