Results 1 to 14 of 14

Thread: Loading and Rotating an image with respect to one coordinate.

  1. #1
    Join Date
    Mar 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded

    Default Loading and Rotating an image with respect to one coordinate.

    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....

  2. #2
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Loading and Rotating an image with respect to one coordinate.

    Post the code you are using. So that we can suggest


    Added after 4 minutes:



    Qt Code:
    1. painter->translate(100, 150); // the point about which the image rotates
    2. painter->rotate(60);
    To copy to clipboard, switch view to plain text mode 

    Did you used in this way???


    Qt Code:
    1. #include <qimage.h>
    2. #include <qwmatrix.h>
    3.  
    4. QImage myImage("myimage.jpeg");
    5. if( !myImage.isNull() )
    6. {
    7. QWMatrix m;
    8. m.rotate( 90.0 );
    9. myImage = myImage.xForm( m );
    10. }
    To copy to clipboard, switch view to plain text mode 


    Or this way
    Last edited by wysota; 21st March 2011 at 08:13.

  3. #3
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Loading and Rotating an image with respect to one coordinate.

    Please use tags around the code u post.

  4. #4
    Join Date
    Mar 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded

    Default Re: Loading and Rotating an image with respect to one coordinate.

    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....

  5. #5
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Loading and Rotating an image with respect to one coordinate.

    Hm,try this (for instance):

    Qt Code:
    1. QLabel label=new QLabel;
    2.  
    3. {
    4. QMatrix matrix;
    5.  
    6. matrix.rotate(degrees);
    7.  
    8. QPixmap pixmap("file.ext");
    9.  
    10. pixmap=pixmap.transformed(matrix);
    11.  
    12. label->setPixmap(pixmap);
    13. }
    14. layout->addWidget(label);
    To copy to clipboard, switch view to plain text mode 


    If you want to use painter for drawing the image,take a look at the assistant,
    Last edited by wysota; 21st March 2011 at 08:14. Reason: missing [code] tags

  6. #6
    Join Date
    Mar 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded

    Default Re: Loading and Rotating an image with respect to one coordinate.

    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.....

    Qt Code:
    1. void rotate::paintEvent(QPaintEvent *)
    2. {
    3. QMatrix matrix;
    4. matrix.rotate(00);
    5. QPixmap pixmap("tri.jpg");
    6. pixmap=pixmap.transformed(matrix);
    7. this->ui->label->setPixmap(pixmap);
    8. QHBoxLayout* layout=new QHBoxLayout(this);
    9. layout->addWidget(this->ui->label);
    10. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Loading and Rotating an image with respect to one coordinate.

    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.

  8. #8
    Join Date
    Mar 2011
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Loading and Rotating an image with respect to one coordinate.

    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'.

  9. #9
    Join Date
    Mar 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded

    Default Re: Loading and Rotating an image with respect to one coordinate.

    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.....


    Qt Code:
    1. QMatrix matrix;
    2. matrix.translate(10,10);
    3. matrix.rotate(00);
    4. matrix.translate(20,30);
    5. QPixmap pixmap("tri.jpg");
    6. pixmap=pixmap.transformed(matrix);
    7. this->ui->label->setPixmap(pixmap);
    8. QHBoxLayout* layout=new QHBoxLayout(this);
    9. layout->addWidget(this->ui->label);
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Mar 2011
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Loading and Rotating an image with respect to one coordinate.

    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
    Qt Code:
    1. QPixmap pixmap("tri.jpg");
    2.  
    3. QMatrix matrix;
    4. matrix.translate(pixmap.width/2,pixmap.height);
    5. matrix.rotate(90);
    6.  
    7.  
    8. pixmap=pixmap.transformed(matrix);
    9. this->ui->label->setPixmap(pixmap);
    10. QHBoxLayout* layout=new QHBoxLayout(this);
    11. layout->addWidget(this->ui->label);
    To copy to clipboard, switch view to plain text mode 

    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.ph..._picture_in_Qt for details.

  11. #11
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Loading and Rotating an image with respect to one coordinate.

    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.

  12. #12
    Join Date
    Mar 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded

    Default Re: Loading and Rotating an image with respect to one coordinate.

    Thanx a lot.... I could do it....

  13. #13
    Join Date
    Mar 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded

    Default Re: Loading and Rotating an image with respect to one coordinate.

    hi...
    I have two images, which i have loaded on the QLabel through PIXMAP...

    Qt Code:
    1. QPixmap p1("image1.jpg");
    2. this->ui->label1->setPixmap(p1);
    3.  
    4. QPixmap p2("image2.jpg");
    5. this->ui->label2->setPixmap(p2);
    To copy to clipboard, switch view to plain text mode 

    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....

  14. #14
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Loading and Rotating an image with respect to one coordinate.

    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.

Similar Threads

  1. Loading an image in a thread
    By DarkyDuck in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2011, 18:28
  2. Loading a raw image into QImage
    By Luc4 in forum Qt Programming
    Replies: 6
    Last Post: 16th May 2010, 15:20
  3. loading image from disk
    By freekill in forum Qt Programming
    Replies: 1
    Last Post: 18th February 2010, 05:21
  4. Image Loading
    By kavinsiva in forum Qt Programming
    Replies: 2
    Last Post: 11th August 2009, 08:00
  5. Paint Rect visible on Drag to crop image coordinate
    By patrik08 in forum Qt Programming
    Replies: 8
    Last Post: 17th March 2007, 09:04

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
  •  
Qt is a trademark of The Qt Company.