Problem to rotate a QGraphicRectItem
-
My problem is when I rotate a rect, this no rotate very well. My centerPoint is (0, 0) corner left-up, but only rotate good when call it twice. I reimplemented mousepressEvent() for to call rotate(90) only.
Please help me how to rotate properly an Item. -
It would be easier to help you if you provided more information, e.g.
- post the code that you use
- explain what you expect to happen
- explain what you see happening
-
Excuse me.
I want to rotate the rectangle around its point of origin (0, 0) top-left, but when I click the item does not work properly (Figure A). Just after 3 click does what I expected (Figure B). How do I rotate at the first click.
Another issue is that without the variable num, rotates only once, when I try to make another click does nothing. I have broken my head but I can't get results.
My Qt version is Qt 5.0.1
@
#include <QApplication>
#include <QtWidgets>
#include "rectitem1.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);QGraphicsView *view = new QGraphicsView; QGraphicsScene *scene = new QGraphicsScene; RectItem1 *item = new RectItem1; item->setRect(QRect(0, 0, 100, 200)); scene->addItem(item); view->setScene(scene); view->resize(600, 600); view->show(); return a.exec();
}
#ifndef RECTITEM1_H
#define RECTITEM1_H#include <QGraphicsRectItem>
class RectItem1 : public QGraphicsRectItem
{
public:
RectItem1();protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);};
#endif // RECTITEM1_H
#include "rectitem1.h"
RectItem1::RectItem1()
{
}void RectItem1::mousePressEvent(QGraphicsSceneMouseEvent event)
{
static int num = 0;
setRotation(90++num);
QGraphicsRectItem::mousePressEvent(event);
}
@"Found Image":http://www.subirimagenes.com/otros-function-8689022.html
-
I have just compiled your code
works perfectlyyou have to click inside the rectangular item that you are adding
otherwise mouse event will not be passed to the RectItem1 object