QPainter drawRect 到现有图像绘制圆形边缘而不是尖锐边缘。如何画出锋利的边缘?

QPainter drawRect to existing image draws rounded edges instead of sharp edges. How to draw sharp edges?

我的目标是将图形绘制到图像上并保存到文件中。在这个简化的示例中,我为 QPainter 准备了一个图像,然后通过 drawRect 绘制一个矩形到现有图像并保存到文件中。生成的图像显示组合图像,但矩形显示圆角边缘而不是尖锐边缘。如何解决?感谢您的帮助。

include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPainter>
#include <QImage>
#include <QPen>
void paint_Object(QPainter *painter)
{
    QPen pen;
    pen.setWidth(40);
    pen.setColor(Qt::red);
    QRect rect;

    painter->setPen(pen);
    rect.setTopLeft(QPoint(100,100));
    rect.setWidth(100);
    rect.setHeight(200);
    painter->drawRect(rect);
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString filename_in = "C:/Users/seany/VideoAd/temp/test.png";
    QString filename_out = "C:/Users/seany/VideoAd/temp/test_out.png";

    //Setup initial
    QImage test_image(filename_in);

    //Setup Painter with initial Image
    QPainter painter(&test_image);

    //Paint an overlaid image onto initial image
    paint_Object(&painter);

    //Write resultant image to file
    test_image.save(filename_out);
}

MainWindow::~MainWindow()
{
    delete ui;
}

QPen 连接样式的默认值为 Qt::BevelJoin。这就是它即将到来的圆边。使用 pen.setJoinStyle(Qt::MiterJoin) 表示延伸或锐边。 有关更多信息,请阅读 this