以下是使用 Qt widget 控件显示图像并旋转 90 度的示例代码:

#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtGui/QPixmap>
#include <QtGui/QPainter>
#include <QtGui/QTransform>

class ImageWidget : public QWidget {
public:
    ImageWidget(QWidget* parent = nullptr) : QWidget(parent) {}

    void setImage(const QPixmap& pixmap) {
        m_pixmap = pixmap;
        update();
    }

protected:
    void paintEvent(QPaintEvent* event) override {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setRenderHint(QPainter::SmoothPixmapTransform);

        // 旋转 90 度
        QTransform transform;
        transform.rotate(90);
        painter.setTransform(transform);

        painter.drawPixmap(QPoint(0, 0), m_pixmap);
    }

private:
    QPixmap m_pixmap;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QPixmap pixmap('image.jpg');

    ImageWidget widget;
    widget.setImage(pixmap);
    widget.show();

    return app.exec();
}

在上面的代码中,我们创建了一个名为 ImageWidget 的自定义 QWidget 子类,并在其中实现了 paintEvent 函数来进行绘制。在 paintEvent 函数中,我们首先设置了渲染选项,然后使用 QTransform 进行 90 度旋转。最后,我们使用 QPainter 的 drawPixmap 函数来绘制图像。

在 main 函数中,我们创建了一个 QPixmap 对象来加载图像文件,并将其传递给 ImageWidget 的 setImage 函数来显示图像。最后,我们调用 QApplication 的 exec 函数来运行应用程序。

Qt Widget 控件图像旋转 90 度示例

原文地址: https://www.cveoy.top/t/topic/nk6S 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录