9

In my application I want to rotate image (I have set image on QLabel). I have set one QPushButton, on click that button I want to rotate my image in Four directions (Right->Bottom->Left->Top)

Any help?

2 Answers 2

21

Assuming you have a pointer to your QLabel you could do something like

void MyWidget::rotateLabel()
{
    QPixmap pixmap(*my_label->pixmap());
    QMatrix rm;
    rm.rotate(90);
    pixmap = pixmap.transformed(rm);
    my_label->setPixmap(pixmap);
}

This will take you through Right, Bottom, Left, Top in four applications.

3
  • 1
    i get this for the first line where you fill the newly pixmap with my_label->pixmap(): 'QPixmap::QPixmap(QPixmapData *)' : cannot convert parameter 1 from 'const QPixmap *' to 'QPixmapData *' "
    – PathOfNeo
    Commented Mar 5, 2013 at 20:19
  • 5
    +1: Cool works! I'd only allow me to add one remark. QMatrix is meanwhile depricated. Replacing QMatrix by QTransform would comply with Qt4.8, Qt5 better.
    – Valentin H
    Commented Jun 26, 2013 at 15:08
  • 2
    Don't do this with images loaded from disk and save, you'll lose the metadata.
    – Gabriel
    Commented Jan 15, 2020 at 2:51
6

QMatrix is deprecated so you can use QTransform instead

void MyWidget::rotateLabel()
{
    QPixmap pixmap(*my_label->pixmap());
    QTransform tr;
    tr.rotate(90);
    pixmap = pixmap.transformed(tr);
    my_label->setPixmap(pixmap);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.