这是我试过的,它没有给我任何输出。我哪里错了?
// Start point of bottom line
qreal startPointX1 = 600.0;
qreal startPointY1 = 600.0;
// End point of bottom line
qreal endPointX1 = 600.0;
qreal endPointY1 = 1200.0;
// Start point of top line
qreal startPointX2 = 600.0;
qreal startPointY2 = 600.0;
// End point of top line
qreal endPointX2 = 800.0;
qreal endPointY2 = 1200.0;
QPainterPath path;
// Set pen to this point.
path.moveTo (startPointX1, startPointY1);
// Draw line from pen point to this point.
path.lineTo (endPointX1, endPointY1);
path.moveTo (endPointX1, endPointY1);
path.lineTo (endPointX2, endPointY2);
path.moveTo (endPointX2, endPointY2);
path.lineTo (startPointX1, startPointY1);
painter.setPen (Qt :: NoPen);
painter.fillPath (path, QBrush (QColor ("blue")));
我刚尝试在这三个点之间创建一条路径并填充该区域,但没有显示输出。
答案 0 :(得分:12)
我认为您致电moveTo()
后无需拨打lineTo()
功能,因为当前位置已更新到您绘制的线的终点。这是为我绘制矩形的代码:
// Start point of bottom line
qreal startPointX1 = 600.0;
qreal startPointY1 = 600.0;
// End point of bottom line
qreal endPointX1 = 600.0;
qreal endPointY1 = 1200.0;
// Start point of top line
qreal startPointX2 = 600.0;
qreal startPointY2 = 600.0;
// End point of top line
qreal endPointX2 = 800.0;
qreal endPointY2 = 1200.0;
QPainterPath path;
// Set pen to this point.
path.moveTo (startPointX1, startPointY1);
// Draw line from pen point to this point.
path.lineTo (endPointX1, endPointY1);
//path.moveTo (endPointX1, endPointY1); // <- no need to move
path.lineTo (endPointX2, endPointY2);
//path.moveTo (endPointX2, endPointY2); // <- no need to move
path.lineTo (startPointX1, startPointY1);
painter.setPen (Qt :: NoPen);
painter.fillPath (path, QBrush (QColor ("blue")));
答案 1 :(得分:6)
如果您想使用QRectF
QRectF rect = QRectF(0, 0, 100, 100);
QPainterPath path;
path.moveTo(rect.left() + (rect.width() / 2), rect.top());
path.lineTo(rect.bottomLeft());
path.lineTo(rect.bottomRight());
path.lineTo(rect.left() + (rect.width() / 2), rect.top());
painter.fillPath(path, QBrush(QColor ("blue")));
答案 2 :(得分:1)
文档说:&#34;移动当前点也将启动一个新的子路径(在启动新路径时隐式关闭先前的当前路径)&#34;。
这意味着你应该移动到路径的原点,然后只使用lineTo来绘制要填充的形状。
我添加了这个答案,因为答案&#34;我认为你在调用lineTo()之后不需要调用moveTo()函数,因为当前位置已经更新到你绘制的行的结束点。 #34;是非常误导。移动并不是必需的,它实际上导致了问题。