PDA

View Full Version : Loading and Rotating an image with respect to one coordinate.



Meghana
18th March 2011, 12:15
hi all,

I am a newbie in QT. I am using QT 4.6

My problem is that I am trying to rotate an image with one end fixed. Ex. I have an image of a rocket which I am trying to rotate. I want the base of the rocket to remain at the same position when I rotate the image.

Can you please let me know how to rotate an image with one point fixed.

thanks in advance....:)

Gokulnathvc
18th March 2011, 13:03
Post the code you are using. So that we can suggest

Added after 4 minutes:



painter->translate(100, 150); // the point about which the image rotates
painter->rotate(60);

Did you used in this way???



#include <qimage.h>
#include <qwmatrix.h>

QImage myImage("myimage.jpeg");
if( !myImage.isNull() )
{
QWMatrix m;
m.rotate( 90.0 );
myImage = myImage.xForm( m );
}


Or this way

Archa4
18th March 2011, 13:25
Please use
tags around the code u post.

Meghana
21st March 2011, 05:50
Thanks for replying....But I am still not able to rotate the image....

I have created a resource file, where i have added image.jpg....
In the UI, I have added a label on which i have added the image using CHANGE STYLESHEET option....
now can anybody tell me how to rotate this image....

AlexSudnik
21st March 2011, 08:09
Hm,try this (for instance):


QLabel label=new QLabel;

{
QMatrix matrix;

matrix.rotate(degrees);

QPixmap pixmap("file.ext");

pixmap=pixmap.transformed(matrix);

label->setPixmap(pixmap);
}
layout->addWidget(label);


If you want to use painter for drawing the image,take a look at the assistant,

Meghana
21st March 2011, 12:05
Hmm...This code is rotating the Label, but not with respect to one fixed point....
can anybody help me rotating it with respect to one fixed point.....


void rotate::paintEvent(QPaintEvent *)
{
QMatrix matrix;
matrix.rotate(00);
QPixmap pixmap("tri.jpg");
pixmap=pixmap.transformed(matrix);
this->ui->label->setPixmap(pixmap);
QHBoxLayout* layout=new QHBoxLayout(this);
layout->addWidget(this->ui->label);
}

AlexSudnik
21st March 2011, 13:00
Even if there is no such a built-in method,you can write it by yourself.Point's rotation along the pivot point is a classic trigonometry problem,it's pretty straight.Even if you failed to solve it by yourself,there are a lot of resources on the net regarding it.

pradeeps
21st March 2011, 13:26
I think you can load the image with QImageReader to a label and call the paint event after translation followed by a rotation to a point of your interest.

Remember you need to translate the image before and after rotation to a point which you want to use as the 'center of rotation'.

Meghana
22nd March 2011, 06:22
hmmm .... Is there any other better alternative to keep one co-ordinate fixed, even after rotating the image......
Though I translated the image before and after rotation.....there is no change in the output..:(
it seems a bit difficult to know the X and Y parameters to be passed to translate function as i want to display the image for every two degree of rotation from 0deg to 90deg.....
I jus used translate in this way.... but no difference in output.....:(



QMatrix matrix;
matrix.translate(10,10);
matrix.rotate(00);
matrix.translate(20,30);
QPixmap pixmap("tri.jpg");
pixmap=pixmap.transformed(matrix);
this->ui->label->setPixmap(pixmap);
QHBoxLayout* layout=new QHBoxLayout(this);
layout->addWidget(this->ui->label);

pradeeps
22nd March 2011, 11:16
In your code above the translation quantity given is too small compared to the image resolution which wont produce any noticeable output, and Im confused with the rotation angle given(00).

Try this

QPixmap pixmap("tri.jpg");

QMatrix matrix;
matrix.translate(pixmap.width/2,pixmap.height);
matrix.rotate(90);


pixmap=pixmap.transformed(matrix);
this->ui->label->setPixmap(pixmap);
QHBoxLayout* layout=new QHBoxLayout(this);
layout->addWidget(this->ui->label);



PS: Try and see the effect of translation and rotation, here i removed the re-translation after rotation to make the image not to go back to (0,0) as a result you will be getting a clipped one.

refer http://wiki.forum.nokia.com/index.php/CS001514_-_Rotate_picture_in_Qt for details.

SixDegrees
22nd March 2011, 11:46
I'd suggest taking a small step back and learning about the geometry of simple transformations like this. Rotation about an arbitrary point ALWAYS requires a translation followed by a rotation; there's no getting around it, and demands for "a better alternative" belie a fundamental ignorance of the problem that needs to be solved. No solution arrived at in this way is going to be optimal or maintainable.

Meghana
24th March 2011, 06:35
Thanx a lot.... I could do it....

Meghana
25th March 2011, 07:34
hi...
I have two images, which i have loaded on the QLabel through PIXMAP...


QPixmap p1("image1.jpg");
this->ui->label1->setPixmap(p1);

QPixmap p2("image2.jpg");
this->ui->label2->setPixmap(p2);


I need to place one image on the other...
But if I try to do so,the problem is one image is getting overlapped with the other...
As much as I understood, we can overcome this problem by making the pixmap background transparent....
can anybody plz tel me how can i do that and also if there are any other solution plz do suggest.....

thanks in advance....

AlexSudnik
25th March 2011, 07:52
It's not quite clear what you're trying to achieve,if you just want to place an image on a label it's fine.But if you're looking a way to display and compose the images:i don't think it's the right choice to use QLabel for it.

Take a look at the Assistant's chapter called "Paint System".Some great demos also available from the "Painting" and "Graphics View" sections.