Results 1 to 10 of 10

Thread: rotate an image

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default rotate an image

    I have two images, one inside another. first I used two QFrame s and set background image via stylesheets.
    but this was not a good idea as QFrame has no pixmap() member.
    I changed QFrame to QLabel n set the background image of the inner image via stylesheets. (only the inner image shall rotate).

    Now, I used the following code to try rotating QLabel background image, though I get segmentation fault, when assignig QLabel pixmap to the Qpixmap variable.

    Qt Code:
    1. QPixmap pixmap(*ui->label_11->pixmap());
    2. QMatrix rm;
    3. rm.rotate(90);
    4. pixmap = pixmap.transformed(rm);
    5. ui->label_11->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rotate an image

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    Android

    Default Re: rotate an image

    Im not a professional programmer, but there is something wrong with your code, check this image ratating sample code and have a comparison to check,hope this can help you.

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: rotate an image

    as I said I have one QLabel inside a QFrame. the label background image is a small movable part of the general frame background image. what I want is to rotate the smaller image according to a particular angle.

    Qt Code:
    1. void MainWindow::rotate()
    2. {
    3. QRectF target(0.0, 0.0, ui->label->width(), ui->label->height());
    4. QRectF source(0.0, 0.0, ui->label->width(), ui->label->height());
    5. QPixmap pixmap("path-to-image");
    6.  
    7. QPainter painter(this);
    8. painter.rotate(90.0);
    9.  
    10. painter.drawPixmap(target, pixmap, source);
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    I do not know how to link the pixmap to the label background after rotation!

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,387
    Thanks
    247
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: rotate an image

    What's wrong with QLabel::setPixmap()? Seems like the obvious thing to use after reading the QLabel documentation.

  6. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: rotate an image

    Although I could rotate the inner image with the code below, the sad point is that, the background image always fit inside the inner QLabel. But, what I need is to rotate both the QLabel and the image simultaneously. or it will be better not to use QLabel at all this way. can I only load an extra image inside the outer frame background image and try to rotate it? I need help please.

    Qt Code:
    1. QPixmap *orig_pixmap = new QPixmap();
    2. orig_pixmap->load("path-to-png");
    3. degrees++;
    4. if(degrees == 90) degrees = 0;
    5. QTransform rotate_disc;
    6. rotate_disc.translate((orig_pixmap->width()-ui->label->width())/2 , (orig_pixmap->width()-ui->label->height())/2);
    7. rotate_disc.rotate(degrees,Qt::ZAxis);
    8. QPixmap pixmap;
    9. //pixmap = orig_disc->scaled(89,89,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    10. pixmap = orig_pixmap->transformed(rotate_disc,Qt::SmoothTransformation);
    11. ui->label->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 
    Last edited by saman_artorious; 26th March 2013 at 06:13.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rotate an image

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget {
    4. Q_OBJECT
    5. Q_PROPERTY(int rot1 READ rot1 WRITE setRot1);
    6. Q_PROPERTY(int rot2 READ rot2 WRITE setRot2);
    7. public:
    8. Widget(QWidget *parent = 0) : QWidget(parent) { m_rot1 = 0; m_rot2 = 0; }
    9. void setPixmap1(QPixmap px) { m_px1 = px; update(); }
    10. void setPixmap2(QPixmap px) { m_px2 = px; update(); }
    11. int rot1() const { return m_rot1; }
    12. int rot2() const { return m_rot2; }
    13. public slots:
    14. void setRot1(int r1) { m_rot1 = r1; update(); }
    15. void setRot2(int r2) { m_rot2 = r2; update(); }
    16. protected:
    17. void paintEvent(QPaintEvent *) {
    18. QPainter p(this);
    19. p.setRenderHint(QPainter::Antialiasing);
    20. p.translate(width()/2, height()/2);
    21. if(!m_px1.isNull()) {
    22. p.save();
    23. p.rotate(m_rot1);
    24. QRect r = m_px1.rect();
    25. r.moveCenter(QPoint(0,0));
    26. p.drawPixmap(r, m_px1);
    27. p.restore();
    28. }
    29. if(!m_px2.isNull()) {
    30. p.save();
    31. p.rotate(m_rot2);
    32. QRect r = m_px2.rect();
    33. r.moveCenter(QPoint(0,0));
    34. p.drawPixmap(r, m_px2);
    35. p.restore();
    36. }
    37. }
    38. private:
    39. int m_rot1;
    40. int m_rot2;
    41. QPixmap m_px1, m_px2;
    42. };
    43.  
    44. #include "main.moc"
    45.  
    46. int main(int argc, char **argv) {
    47. QApplication app(argc, argv);
    48. Widget w;
    49. w.setPixmap1(QPixmap("/usr/share/icons/default.kde4/64x64/actions/system-lock-screen.png"));
    50. w.setPixmap2(QPixmap("/usr/share/icons/default.kde4/64x64/actions/application-exit.png"));
    51. w.resize(100,100);
    52. QPropertyAnimation anim1(&w, "rot1");
    53. QPropertyAnimation anim2(&w, "rot2");
    54. anim1.setStartValue(0);
    55. anim2.setStartValue(0);
    56. anim1.setEndValue(359);
    57. anim2.setEndValue(-359);
    58. anim1.setLoopCount(-1);
    59. anim2.setLoopCount(-1);
    60. anim1.setDuration(5000);
    61. anim2.setDuration(3000);
    62. anim1.start();
    63. anim2.start();
    64. w.show();
    65. return app.exec();
    66. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    saman_artorious (26th March 2013)

  9. #8
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: rotate an image

    There is something I need to know. As in my case I have a MainWindow of so many tools n widgets. would it be all right if I inherit from QWidget publicly in MainWindow class?
    that way I can load as much anim n pixmaps as I need in the project main window. The main window paintEvent would be customized then. is that ok?

  10. #9
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: rotate an image

    Wysota
    The the rotation axis of the second image is its center. However, I may want to set one side of the rectangle fixed and or even rotate it from a certain point. Is that possible?

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rotate an image

    Yes, of course. You can manipulate the painter in any way you want using rotate(), scale() and translate().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Rotate a QRectF
    By whitefurrows in forum Qt Programming
    Replies: 5
    Last Post: 15th July 2009, 15:33
  2. how to rotate an image...
    By sh123 in forum Qt Programming
    Replies: 2
    Last Post: 15th January 2009, 14:59
  3. Rotate QLabel
    By shader76 in forum Newbie
    Replies: 9
    Last Post: 24th December 2007, 12:31
  4. Rotate QGraphicsPixmapItem
    By durbrak in forum Qt Programming
    Replies: 7
    Last Post: 15th April 2007, 14:51
  5. Replies: 3
    Last Post: 14th March 2007, 08:09

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.