如何在qt创建者中画出一颗心

时间:2015-01-31 13:58:36

标签: c++ qt draw

我需要在qt创建者上画一个心脏,但是当我使用drawArc时我遇到了问题。我知道我用错了方法,但我不知道该怎么做。请帮帮忙吗?这是用c ++绘制内心的代码。

HeartDrawableStrategy::draw(QPainter &painter, QRect &rect, double xRatio, double yRatio, QPoint &pos, int size,)
{
    //Define the heart points
    int newPointX = pos.x();
    int newPointY = pos.y()+size/4;
    QPoint leftPoint = QPoint(newPointX*xRatio, newPointY*yRatio);

    newPointX = pos.x()+size/2;
    newPointY = pos.y() + size;
    QPoint bottomCenterPoint = QPoint(newPointX*xRatio, newPointY*yRatio);

    newPointX = pos.x() + size;
    newPointY = pos.y() + size/4;
    QPoint rightPoint = QPoint(newPointX*xRatio, newPointY*yRatio);

    newPointX = pos.x() + size/2;
    newPointY = pos.y() + size/4;
    QPoint middlePoint = QPoint(newPointX*xRatio, newPointY*yRatio);

    newPointX = pos.x() + size/4;
    newPointY = pos.y();
    QPoint topLeftPoint = QPoint(newPointX*xRatio, newPointY*yRatio);

    newPointX = pos.x() + (size*3/4);
    newPointY = pos.y();
    QPoint topRightPoint = QPoint(newPointX*xRatio, newPointY*yRatio);

    //Draw the heart
    QLine line = QLine(leftPoint, bottomCenterPoint);
    painter.drawLine(line);

    line = QLine(bottomCenterPoint, rightPoint);
    painter.drawLine(line);

    line = QLine(leftPoint, middlePoint);
    painter.drawArc(line);

    line = QLine(middlePoint, rightPoint);
    painter.drawArc(line);

}

1 个答案:

答案 0 :(得分:1)

您当前正在创建QLine,并尝试使用它创建弧。这不起作用。

根据QT Doc 您需要X中的起点,Y中的起点,宽度,高度,起始角度和跨度角。 像那样:

painter.drawArc(x, y, width, height, startAngle, spanAngle);

X和Y是左上角,startAngle和spanAngle必须以1/16度指定,即整圆等于5760(16 * 360)。角度的正值表示逆时针,而负值表示顺时针方向。零度位于3点位置。

代码如下:

painter.drawArc(10, 20, 80, 60, (30 * 16), (120 * 16);

会画出这个:

drawArc