Results 1 to 6 of 6

Thread: Rotation problem

  1. #1
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Rotation problem

    Hi all,

    I am new to Qt, currently I am using Qt 4 on Linux. I am writing a application similar to the "draggableicons" sample. All I need is icons should rotate say 40 degrees when we right click on it. Can some body explain how to do it? Here is the code I have modified
    in the draggableicons sample, but it doesnt work

    Qt Code:
    1. void DragWidget::mousePressEvent(QMouseEvent *event)
    2. {
    3. .
    4. .
    5. .
    6. QPixmap tempPixmap = pixmap;
    7. QPainter painter;
    8. painter.begin(&tempPixmap);
    9. //rotate abt 60 degrees
    10. painter.rotate(60);
    11. painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
    12. painter.end();
    13. .
    14. .
    15. .
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance,
    Prasanna Bhat
    Last edited by wysota; 17th January 2007 at 17:53. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Rotation problem

    Do not try to paint outside of the paint event.

    have a Variable containing the current rotation which you use in the paint event to redraw the screen. When you register a click, update the variable and request an update.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Rotation problem

    boss_bha, the problem is that you copy a pixmap as it is, then create and rotate the painter, and fill a rotated rectangle over the copied pixmap. So the pixmap is not rotated anywhere in that code.

    Quote Originally Posted by camel View Post
    Do not try to paint outside of the paint event.
    This applies only to QWidgets and he seems to be painting on a QPixmap.
    J-P Nurmi

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Rotation problem

    Here's something to get you going. This might be a bit slow (due to QPixmap->QImage->QPixmap conversions), but close to a working solution:
    Qt Code:
    1. QImage tempImage = pixmap.toImage();
    2. //rotate abt 60 degrees
    3. QMatrix matrix;
    4. matrix.rotate(60);
    5. tempImage = tempImage.transformed(matrix);
    6. QPainter painter;
    7. painter.begin(&tempImage);
    8. painter.fillRect(tempImage.rect(), QColor(127, 127, 127, 127));
    9. painter.end();
    10.  
    11. child->setPixmap(QPixmap::fromImage(tempImage));
    To copy to clipboard, switch view to plain text mode 
    What does it miss is some proper scaling. A rotated image takes naturally more space to get drawn entirely.
    Another way is not to copy the pixmap, but draw it after the painter has been rotated.
    J-P Nurmi

  5. #5
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rotation problem

    Hi JP-N,

    Sorry to bug you again,
    I tried your code but it's not working, Am I wrong here?? I am pasting the entire code of mousePressEvent()

    Qt Code:
    1. void DragWidget::mousePressEvent(QMouseEvent *event)
    2. {
    3. QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
    4. if (!child)
    5. return;
    6.  
    7. QPixmap pixmap = *child->pixmap();
    8.  
    9. QByteArray itemData;
    10. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    11. dataStream << pixmap << QPoint(event->pos() - child->pos());
    12.  
    13. QMimeData *mimeData = new QMimeData;
    14. mimeData->setData("application/x-dnditemdata", itemData);
    15.  
    16. QDrag *drag = new QDrag(this);
    17. drag->setMimeData(mimeData);
    18. drag->setPixmap(pixmap);
    19. drag->setHotSpot(event->pos() - child->pos());
    20.  
    21.  
    22. if (event->button()==Qt::RightButton){
    23. QImage tempImage = pixmap.toImage();
    24. //rotate abt 60 degrees
    25. QMatrix matrix;
    26. matrix.rotate(60);
    27. tempImage = tempImage.transformed(matrix);
    28. QPainter painter;
    29. painter.begin(&tempImage);
    30. painter.fillRect(tempImage.rect(),
    31. QColor(127, 127, 127, 127));
    32. painter.end();
    33. child->setPixmap(QPixmap::fromImage(tempImage));
    34. }
    35. else
    36. {
    37. QPixmap tempPixmap = pixmap;
    38. QPainter painter;
    39. painter.begin(&tempPixmap);
    40. painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
    41. painter.end();
    42. child->setPixmap(tempPixmap);
    43.  
    44. }
    45. if (drag->start(Qt::CopyAction | Qt::MoveAction) == Qt::MoveAction)
    46. child->close();
    47. else {
    48. child->show();
    49. child->setPixmap(pixmap);
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance,
    Prasanna Bhat
    Last edited by wysota; 17th January 2007 at 17:52. Reason: missing [code] tags

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Rotation problem

    Could you elaborate what you intend to do? If you plan to rotate the same image multiple times in a row, I'd suggest you to use some another approach. Like for example keeping the original image around..
    J-P Nurmi

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 13:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 13:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 15:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 22:36
  5. Replies: 16
    Last Post: 7th March 2006, 16: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.