【发布时间】:2014-08-03 09:43:02
【问题描述】:
我正在尝试绘制一个矩形。矩形每个边框的颜色、粗细和笔样式必须是可定制的。例如,我必须能够绘制一个矩形,其左边框粗细为 5,颜色为红色,右边框粗细 = 6,颜色为蓝色,依此类推。所以我决定画4条不同的线并将它们连接起来形成一个矩形。下面的函数在给定原点、边框粗细和矩形宽度和高度的情况下绘制一个具有不同边框的矩形。左右边框必须在顶行空间之后开始。 (y = originY+topThickness)并在底部边框之前结束(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);
}
当我画线时,我得到如下线。
如您所见,线的连接和线的起点和终点并不像预期的那样。例如,左线不应与顶线重叠。
我做错了什么?如何绘制具有不同边框(在厚度方面)的矩形,其中边框连接整齐而没有重叠?提前致谢。
【问题讨论】: