Results 1 to 7 of 7

Thread: Transform(move) Circle starting point - it needs to rotate and stay fixed

  1. #1
    Join Date
    Oct 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Transform(move) Circle starting point - it needs to rotate and stay fixed

    hi, i'm new to QT (ofcourse): i have a problem and don't know if it is the right aproach.

    In short: I just want the circle to rotate and stay fixed - instead of dancing
    I have a Qmainwindow, in the middle there is a graphicview, on the scene there is a circle divided in 4 pieces (2 lines) and a button "rotate". So i need the circle to rotate when you push the button.

    Because i don't know yet how to deal with time (e.g. rotating the circle for N seconds) i rotate it for 30 degrees. The problem is that i need the circle to be fixed and the starting point to be in the middle of the circle, so that when i push the button it rotates around his own axis. I hope you understand what i mean.
    Now it's rotating around the default starging point (0,0) i upper left corner of my BoundingRect so the circle isn't fixed but rather it rotates all over the view around the default (0,0) instead of staying fixed and rotating on place.

    Here is a picture what i mean:



    I think i need to transform the default starting point (0,0) of my QRectF to the middle of my circle -> so that what it will stay fixed and rotate around his own axis. I'm not sure if i've got it right.

    Code:

    Qt Code:
    1. //constructor of main class (RuletV1.cpp)
    2. RuletV1::RuletV1(QWidget *parent, Qt::WFlags flags)
    3. : QMainWindow(parent, flags)
    4. {
    5. ui.setupUi(this);
    6. ui.graphicsView->setScene(scena);
    7. m_elipsa = new CircleItem(); //note: QGraphicsItem *m_elipsa;
    8. scena->addItem(m_elipsa);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //push method
    2. void RuletV1::on_pushButton_clicked()
    3. {
    4. dynamic_cast<CircleItem*>(m_elipsa)->rotate(30); // is there better way rotating?
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //circeitem.cpp
    2. #include "circleitem.h"
    3.  
    4. //constructor
    5. CircleItem::CircleItem(QGraphicsItem *parent)
    6. : QGraphicsItem(parent)
    7. {}
    8. //destructor
    9. CircleItem::~CircleItem()
    10. {}
    11. //return QRectF
    12. QRectF CircleItem::boundingRect() const
    13. {
    14. return QRectF(0,0,100,100);
    15. }
    16. //Paint
    17. void CircleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    18. QWidget *widget)
    19. {
    20. painter->setRenderHint(QPainter::Antialiasing);
    21. painter->drawLine(0,50, 100, 50); //line one
    22. painter->drawLine(50, 0, 50, 100); //line two
    23. painter->drawEllipse(0, 0, 100, 100); //circle
    24. }
    To copy to clipboard, switch view to plain text mode 

    p.s: i'v excluded some .h files as they are not so crucial right now

    Qt Code:
    1. //main.cpp
    2. int main(int argc, char *argv[])
    3. {
    4. QApplication a(argc, argv);
    5. RuletV1 w;
    6. w.show();
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    I hope this is enough info - thx in advanced

  2. #2
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Transform(move) Circle starting point - it needs to rotate and stay fixed

    Qt Code:
    1. //push method
    2. void RuletV1::on_pushButton_clicked()
    3. {
    4. dynamic_cast<CircleItem*>(m_elipsa)->rotate(30); // is there better way rotating?
    5. }
    To copy to clipboard, switch view to plain text mode 


    Yep, what it does is the normal behavour; so you need to translate it first.

    From docs:

    QGraphicsItem::rotate ( qreal angle )
    So, in your case, x and y should be width/2 and height/2 of your item.

  3. #3
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Transform(move) Circle starting point - it needs to rotate and stay fixed

    You can use QTransform for this.
    Qt Code:
    1. QTransform tra;
    2. qreal dx; //Set is properly according the radius of your circle.
    3. qreal dy; //Set is properly according the radius of your circle.
    4. tra.translate(dx,dy);
    5. tra.rotate(qreal angle);
    6. tra.translate(-dx, -dy);
    7. dynamic_cast<CircleItem*>(m_elipsa)->setTransform(tra);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transform(move) Circle starting point - it needs to rotate and stay fixed

    Yes thx, that does it. But it only works for 1 time, it doesn't rotate a second time.

    Qt Code:
    1. void RuletV1::on_pushButton_clicked()
    2. {
    3. dynamic_cast<CircleItem*>(m_elipsa)->setTransform(QTransform().translate(50,50).rotate(30).translate(-50,-50));
    4. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Transform(move) Circle starting point - it needs to rotate and stay fixed

    Try to save your current Angle in a member variable, add 30 degrees each time you press the button and rotate it later that variable.

    Maybe "it rotates until degree x", no "it rotates x degrees".

  6. #6
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Transform(move) Circle starting point - it needs to rotate and stay fixed

    Quote Originally Posted by jano_alex_es View Post
    Try to save your current Angle in a member variable, add 30 degrees each time you press the button and rotate it later that variable.
    Maybe "it rotates until degree x", no "it rotates x degrees".
    Absolutely right.

  7. #7
    Join Date
    Oct 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transform(move) Circle starting point - it needs to rotate and stay fixed

    Yes thanks guys - it works wonderfull

    Code.

    Qt Code:
    1. void RuletV1::on_pushButton_clicked()
    2. {
    3. QTransform tra;
    4. qreal dx = 50;
    5. qreal dy = 50;
    6. tra.translate(dx,dy);
    7. tra.rotate(Sumarum(30)); //every time add 30 and return
    8. tra.translate(-dx,-dy);
    9. dynamic_cast<CircleItem*>(m_elipsa)->setTransform(tra);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. qreal RuletV1::Sumarum(qreal num) { return myangle+=num; }
    To copy to clipboard, switch view to plain text mode 

    //public: qreal Sumarum(qreal Broj);
    //private: qreal myangle;

    ................
    Side questions -> just a little direction needed

    1) If i recall CSS i would make elastic design web pages - i need to do the same in QT: Resizing the window (QMainWindow) needs to resize all elements on the view (QGraphicsView)

    2) If i want to rotate my circle for a few seconds, what are my possibilites? Is there a built-in mechanism? wich class if apropriate
    .........................

    THX

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.