PDA

View Full Version : QTransform rotate on X axis using QSlider



mvbhavsar
24th July 2012, 08:51
Hi,

I have QGraphicsRectItem on my scene and now I want to rotate this rectangle around x or y axis besides default z axis. I want this to be acheived using QSlider which has a range of 0 to 360. When I slides the slider its value is passed to QTransform as an angle to rorate and respective axis parameter is mentioned in rotate method. This rotates the rectangle but it is not to the angle as it should be, particulary when we are reversing slider it should go back to original state and once slider value is 0 it should be as it before we start i.e. no rotation. But it is giving wiered behviour and not rorating as per expectation.

Can somebody help me out.

Thanks in advance


Manish

wysota
25th July 2012, 18:57
Please post a minimal compilable example reproducing the problem.

mvbhavsar
25th July 2012, 19:20
Please find the code which I am writing.






void ShapeSetting::updateItemRotation(qreal r)
{
Rectangle *vrect;
int typ;
QTransform transform;
qreal m_rotation_x;
qreal m_rotation_y;
qreal m_rotation_z;
QPointF center;

qreal old_rot_x = m_rotation_x;
qreal old_rot_y = m_rotation_y;
qreal old_rot_z = m_rotation_z;
m_rotation_x = ui->xSpin->value();
m_rotation_y = ui->ySpin->value();
m_rotation_z = ui->zSpin->value();

transform.translate(50,50);
transform.rotate(m_rotation_x - old_rot_x,Qt::XAxis);
transform.rotate(m_rotation_y - old_rot_y,Qt::YAxis);
transform.rotate(m_rotation_z - old_rot_z,Qt::ZAxis);
transform.translate(-50,-50);
switch(typ){
case 3:
vrect->transform().reset();
vrect->setTransform(transform,true);
break;
}
update();
}

void ShapeSetting::on_xSlider_valueChanged(int value)
{
updateItemRotation(value);
}

void ShapeSetting::on_ySlider_valueChanged(int value)
{
updateItemRotation(value);
}

void ShapeSetting::on_zSlider_valueChanged(int value)
{
updateItemRotation(value);
}

wysota
25th July 2012, 19:22
This is not a compilable example.

mvbhavsar
26th July 2012, 15:23
Please find attached code



Manish

high_flyer
26th July 2012, 18:16
The only reason something at all is changing is because you are unsing non initialized variables that give any garabage values they got at definition.
The fact you are not using the input parameter of updateItemRotation() doesn't help much either.
Then:


QTransform transform;
qreal m_rotation_x; //defining local variables. (the m_ notation suggests this should have a been a member - did you copy this?)
qreal m_rotation_y;
qreal m_rotation_z;
QPointF center;

qreal old_rot_x = m_rotation_x; //You are setting the non initialized variables in on other local variables that will die when the function ends.
qreal old_rot_y = m_rotation_y;
qreal old_rot_z = m_rotation_z;
m_rotation_x = ui->xSpin->value();
m_rotation_y = ui->ySpin->value();
m_rotation_z = ui->zSpin->value();


I am also not quite sure I understand what rotating on more than one axis means on a 2D widget?

Seems to me you are clueless about what you are doing, or trying to do.

wysota
26th July 2012, 18:41
Please find attached code
It still doesn't compile.


In file included from properties.cpp:1:0:
properties.h:5:26: fatal error: spindelegate.h: Nie ma takiego pliku ani katalogu

Please post a minimal (i.e. not containing any code irrelevant to the problem) compilable (i.e. something I can run qmake && make on and it builds and runs) example (i.e. not your main project code) reproducing the problem.

mvbhavsar
27th July 2012, 07:40
Thanks for response from wysota and high_flyer.
I have resolved the issue and attaching complete code for reference.

Also, in reply to high_flyer's remarks, nothing can be done without any intentions and I have firm goals of what I want to achieve, this case was part of one of the very ambitious project. Also, there is much more in rotation about all 3 axis in 2D graphics. It matters when you have different imagination and also if it not much in rotation in 2D then QT would not have given rotation capability about all 3 axis.

I hope, I am able to clear doubts what I want to achieve through my code.


Regards

Manish