I am trying to draw a rectangle. The color,thickness and the pen style for each border of the rectangle must be customizable. For example, I must be able to draw a rectangle with left border thickness say 5 and color being red, and right border thickness = 6 and color being blue and so on. So I decided to draw 4 different lines and connect them to form a rectangle. The below function draws a rectangle with different borders given the origin point, border thicknesses and rectangle width and height. The left and right borders must start after the top line space. (y = originY+topThickness) and end just before bottom border(end Y = orginY+ rectangleHeight -bottomThickness).
void MainWindow::borderTest()
{
QPainter painter(this);
QPen pen;
qint32 topThickness = 6;
qint32 bottomThickness = 7;
qint32 leftThickness = 8;
qint32 rightThickness = 9;
qint32 originX = 15;
qint32 originY = 15;
qint32 rectangleWidth = 300;
qint32 rectangleHeight = 300;
//Top line
pen.setColor("red");
pen.setWidth(topThickness);
painter.setPen(pen);
painter.drawLine(originX,
originY+topThickness/2,
originX+rectangleWidth,
originY+topThickness/2);
//Right line
pen.setWidth(rightThickness);
pen.setColor("blue");
painter.setPen(pen);
painter.drawLine(originX+rectangleWidth,
originY+topThickness,
originX+rectangleWidth,
originY+rectangleHeight-bottomThickness);
//Bottom line
pen.setWidth(bottomThickness);
pen.setColor("green");
painter.setPen(pen);
painter.drawLine(originX+rectangleWidth,
originY+rectangleHeight-bottomThickness,
originX,
originY+rectangleHeight-bottomThickness);
//Left line
pen.setWidth(leftThickness);
pen.setColor("black");
painter.setPen(pen);
painter.drawLine(originX,
originY+rectangleHeight-bottomThickness,
originX,originY+topThickness);
}
When I draw lines, I am getting lines as below.
As you can see, the joining of lines and starting and ending point of lines are not as expected. For example, the left line should not overlap on the top line.
What am I doing wrong? How to draw rectangles with different borders(in terms of thickness) where borders are connected neatly without overlap? Thanks in advance.