Results 1 to 2 of 2

Thread: Line doesnt Rotate on its center

  1. #1
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1

    Question Line doesnt Rotate on its center

    I'm tryin to make a QGraphicsLineItem rotate, but everytime i trigger rotate it moves to another location. I mean, it does rotate, but not in a especific point! I think it its rotation in its QGraphicsView central point. I dunno how to fix it.

    Here is the code:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <Qt>
    4. #include <QGS.h>
    5. #include <QGraphicsScene>
    6. #include <QGraphicsLineItem>
    7. #include <QEvent>
    8. #include <QDropEvent>
    9. #include <QSignalMapper>
    10. #include <QMouseEvent>
    11. #include <QtDebug>
    12. #include <QWidget>
    13. #include <QList>
    14. #include <math.h>
    15. #include <QtCore/qmath.h>
    16.  
    17.  
    18. MainWindow::MainWindow(QWidget *parent) :
    19. QMainWindow(parent),
    20. ui(new Ui::MainWindow)
    21. {
    22. ui->setupUi(this);
    23. MainWindow::setCentralWidget(ui->view);
    24. scene = new QGS(this);
    25. scene->setSceneRect(MainWindow::geometry());
    26. ui->view->setSceneRect(MainWindow::geometry());
    27.  
    28. QObject::connect(ui->action_Line,SIGNAL(triggered()),this,SLOT(criarLinha()));
    29. QObject::connect(ui->action_New,SIGNAL(triggered()),this,SLOT(newFile()));
    30. QObject::connect(ui->actionR_otate,SIGNAL(triggered()),this,SLOT(rotate()));
    31.  
    32. ui->view->setScene(scene);
    33. ui->view->setMouseTracking(true);
    34. setWindowTitle(tr("AutoPac v1.0"));
    35.  
    36.  
    37.  
    38. }
    39.  
    40. //Private Functions
    41. MainWindow::~MainWindow()
    42. {
    43. delete ui;
    44. }
    45.  
    46.  
    47. //Private Slots
    48. void MainWindow::criarLinha()
    49. {
    50.  
    51. scene->setMode(scene->InsertLine);
    52. }
    53.  
    54. void MainWindow::newFile()
    55. {
    56. scene = new QGS(this);
    57. scene->setSceneRect(QRectF(0, 0, 512, 600));
    58. ui->view->setScene(scene);
    59. }
    60.  
    61. void MainWindow::rotate()
    62. {
    63. int i = 0;
    64. // qreal x1,y1,x2,y2;
    65.  
    66. // x1 = 0;
    67. // y1 = 0;
    68. // x2 = 0;
    69. // y2 = 0;
    70.  
    71. // double pi = 3.1415;
    72.  
    73. for (i = 0 ; i < scene->selectedItems().length() ; i++)
    74. {
    75. if(scene->selectedItems().at(i)->type() == QGraphicsLineItem::Type)
    76. {
    77.  
    78. scene->selectedItems().at(i)->rotate(90); //90 is just an example
    79. scene->selectedItems().at(i)->setSelected(true);
    80.  
    81.  
    82. }
    83. }
    84.  
    85. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Line doesnt Rotate on its center

    Check this out, use setTransformOriginPoint(), and setRotation()
    Qt Code:
    1. //main.cpp
    2. #include <QtGui>
    3. // QGraphicsLineItem rotate
    4. class MyWidget : public QMainWindow
    5. {
    6. Q_OBJECT
    7. public:
    8. MyWidget(QWidget* parent = 0)
    9. : QMainWindow(parent)
    10. , scene(new QGraphicsScene(this))
    11. , view(new QGraphicsView(scene, this))
    12. , action_New(menuBar()->addAction("New", this, SLOT(newLine())))
    13. , action_Rotate(menuBar()->addAction("Rotate", this, SLOT(rotateLine())))
    14. {
    15. setCentralWidget(view);
    16. startTimer(100);
    17. }
    18. private slots:
    19. void newLine()
    20. {
    21. QGraphicsLineItem* item = scene->addLine(0, 0, 100, 100, QPen(QBrush(Qt::red), 4));
    22. item->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    23. item->setTransformOriginPoint(item->boundingRect().center());
    24. }
    25.  
    26.  
    27. void rotateLine()
    28. {
    29. for(int i = 0 ;i < scene->selectedItems().count() ;i++)
    30. if(scene->selectedItems().at(i)->isSelected())
    31. if(scene->selectedItems().at(i)->type() == QGraphicsLineItem::Type)
    32. scene->selectedItems().at(i)->setRotation(scene->selectedItems().at(i)->rotation() + 20);
    33. }
    34. protected:
    35. void timerEvent(QTimerEvent* event)
    36. {
    37. rotateLine();
    38. }
    39.  
    40.  
    41. private:
    42. QAction* action_New;
    43. QAction* action_Rotate;
    44. };
    45. int main(int argc, char *argv[])
    46. {
    47. QApplication a(argc, argv);
    48. MyWidget w;
    49. w.show();
    50. return a.exec();
    51. }
    52. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Read gzip file line-by-line
    By The_Fallen in forum Qt Programming
    Replies: 4
    Last Post: 6th September 2011, 13:41
  2. Replies: 3
    Last Post: 13th August 2010, 11:50
  3. Image reading and writing line by line
    By Astrologer in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2010, 08:15
  4. Replies: 1
    Last Post: 21st November 2009, 08:29
  5. How to rotate an object by its center?
    By tszzp in forum Qt Programming
    Replies: 1
    Last Post: 10th November 2009, 04:25

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.