My goal is to draw figures onto images and save to file. In this simplified example, I prepare an image for QPainter and then draw a rectangle via drawRect to the existing image and save to a file. The resultant image shows the combined image but the rectangle shows rounded edges instead of sharp edges. How to fix it? Your help is appreciated.
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;
}
The default value for QPen join style is Qt::BevelJoin
. That's way its coming round edges. Use pen.setJoinStyle(Qt::MiterJoin)
for extended or sharp edges.
For more information read this.