在Qt中旋转图像

Piy*_*ush 8 user-interface qt

在我的应用程序中,我想旋转图像(我已设置图像QLabel).我已设置一个QPushButton,点击该按钮我想在四个方向上旋转我的图像(右 - >底 - >左 - >顶)

有帮助吗?

dab*_*aid 16

假设你有一个指向QLabel的指针,你可以做类似的事情

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

这将带您通过四个应用程序中的Right,Bottom,Left,Top.

  • +1:酷的作品!我只允许我添加一条评论.QMatrix同时被贬值.用QTransform代替QMatrix将更好地符合Qt4.8,Qt5. (5认同)
  • 不要对从磁盘加载并保存的图像执行此操作,您将丢失元数据。 (2认同)

小智 5

QMatrix已弃用,因此您可以使用QTransform代替

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