Results 1 to 3 of 3

Thread: setTransformOriginPoint

  1. #1
    Join Date
    Aug 2010
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default setTransformOriginPoint

    Can someone please help me figure out how setTransformationOriginPoint works?

    I have a simple program where I am trying to rotate an image using the image edges as the rotation axis. If you press the up arrow, it should rotate along the top axis. The bottom arrow should rotate along the bottom axis, etc.

    Of course, that's not what happens. For each case, it appears that the rotation axis is 0,0. Can someone please show my what I'm doing wrong, or let me know if there is a better way to rotate images along different axis.

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QGraphicsScene>
    4. #include <QHBoxLayout>
    5. #include <QGraphicsView>
    6. #include <QKeyEvent>
    7.  
    8. MainWindow::MainWindow(QWidget *parent)
    9. : QWidget(parent)
    10. {
    11.  
    12. scene->setSceneRect(-150, -150, 300, 300);
    13. scene->setBackgroundBrush(Qt::white);
    14.  
    15. item = new QGraphicsPixmapItem(QPixmap(":/images/0.png"));
    16. item->setPos(QPointF(-item->boundingRect().width()/2,-item->boundingRect().height()/2));
    17. scene->addItem(item);
    18.  
    19.  
    20. view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    21. view->setScene(scene);
    22. view->setSceneRect(-150, -150, 300, 300);
    23. view->setFocus();
    24.  
    25. QHBoxLayout *layout = new QHBoxLayout;
    26. layout->addWidget(view);
    27. setLayout(layout);
    28.  
    29. rotateTimeLine = new QTimeLine(1500, this);
    30. rotateTimeLine->setCurveShape(QTimeLine::LinearCurve);
    31.  
    32. connect(rotateTimeLine, SIGNAL(valueChanged(qreal)),
    33. this, SLOT(updateRotate(qreal)));
    34.  
    35. }
    36.  
    37. MainWindow::~MainWindow()
    38. {
    39.  
    40. }
    41.  
    42.  
    43. void MainWindow::keyPressEvent(QKeyEvent *event)
    44. {
    45. qDebug("MainWindow::keyPressEvent");
    46.  
    47. switch (event->key()) {
    48.  
    49. case Qt::Key_Up:
    50. rotate(0);
    51. break;
    52.  
    53. case Qt::Key_Right:
    54. rotate(1);
    55. break;
    56.  
    57. case Qt::Key_Left:
    58. rotate(2);
    59. break;
    60.  
    61. case Qt::Key_Down:
    62. rotate(3);
    63. break;
    64.  
    65. }
    66. }
    67.  
    68. void MainWindow::rotate(int axis)
    69. {
    70. rotateAxis = axis;
    71. rotateAngle = 0;
    72. rotateTimeLine->start();
    73.  
    74. }
    75.  
    76. void MainWindow::updateRotate(qreal value)
    77. {
    78. QTransform txf = QTransform();
    79.  
    80. rotateAngle = 360*value;
    81.  
    82. switch (rotateAxis)
    83. {
    84. case 0:
    85. // Up arrow - Rotate along top axis
    86. txf.rotate(rotateAngle, Qt::XAxis);
    87. item->setTransformOriginPoint(item->boundingRect().width()/2, item->boundingRect().height()/2);
    88. break;
    89.  
    90. case 1:
    91. // Right arrow - Rotate along rigth axis
    92. txf.rotate(rotateAngle, Qt::YAxis);
    93. item->setTransformOriginPoint(item->boundingRect().width(), item->boundingRect().height()/2);
    94. break;
    95.  
    96. case 2:
    97. // Left arrow - Rotate along left axis
    98. txf.rotate(rotateAngle, Qt::YAxis);
    99. item->setTransformOriginPoint(0, item->boundingRect().height()/2);
    100. break;
    101.  
    102. case 3:
    103. // Down arrow - Rotate along bottom axis
    104. txf.rotate(rotateAngle, Qt::XAxis);
    105. item->setTransformOriginPoint(item->boundingRect().width()/2,item->boundingRect().height());
    106. break;
    107. }
    108.  
    109. item->setTransform(txf, false);
    110. update();
    111. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: setTransformOriginPoint

    Well, your updateRotate logic isn't doing what you think it's doing. By analogy, you are pushing a thumb tack into a piece of construction paper on a cork board, and then rotating the paper around the tack.

    I understand what you *want* to do, but not really how to do it. However, perhaps you should check out the HelloGL example, ignoring the gl-related code and concentrating on the rotation code. Worth a shot?

  3. #3
    Join Date
    Aug 2010
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setTransformOriginPoint

    I have a work around now. I can get the behavior I'm looking for if I do the following:

    * Translate the item to the rotation center point
    * Rotate the item
    * Translate the item back to the starting position

    See the code below for details. I still don't understand why setTransformOriginPoint() doesn't work. Can any Qt experts out there shed some light on this.

    Brian

    Qt Code:
    1. void MainWindow::updateRotate(qreal value)
    2. {
    3. QTransform txf = QTransform();
    4.  
    5. rotateAngle = 360*value;
    6.  
    7. switch (rotateAxis)
    8. {
    9. case 0:
    10. // Up arrow - Rotate along top axis
    11. txf.translate(item->boundingRect().width()/2, 0);
    12. txf.rotate(rotateAngle, Qt::XAxis);
    13. txf.translate(-item->boundingRect().width()/2, 0);
    14. //item->setTransformOriginPoint(item->boundingRect().width()/2, item->boundingRect().height()/2);
    15. break;
    16.  
    17. case 1:
    18. // Right arrow - Rotate along rigth axis
    19. txf.translate(item->boundingRect().width(), item->boundingRect().height()/2);
    20. txf.rotate(rotateAngle, Qt::YAxis);
    21. txf.translate(-item->boundingRect().width(), -item->boundingRect().height()/2);
    22. //item->setTransformOriginPoint(item->boundingRect().width(), item->boundingRect().height()/2);
    23. break;
    24.  
    25. case 2:
    26. // Left arrow - Rotate along left axis
    27. txf.translate(0, item->boundingRect().height()/2);
    28. txf.rotate(rotateAngle, Qt::YAxis);
    29. txf.translate(0, -item->boundingRect().height()/2);
    30. //item->setTransformOriginPoint(0, item->boundingRect().height()/2);
    31. break;
    32.  
    33. case 3:
    34. // Down arrow - Rotate along bottom axis
    35. txf.translate(item->boundingRect().width()/2,item->boundingRect().height());
    36. txf.rotate(rotateAngle, Qt::XAxis);
    37. txf.translate(-item->boundingRect().width()/2,-item->boundingRect().height());
    38. //item->setTransformOriginPoint(item->boundingRect().width()/2,item->boundingRect().height());
    39. break;
    40. }
    41.  
    42. item->setTransform(txf, false);
    43. }
    To copy to clipboard, switch view to plain text mode 

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.