目录

QT-day4

目录

QT day4

1.实现绘图的时候,颜色的随时调整

void Widget::paintEvent(QPaintEvent *event)
{
    painter.begin(this);

    for(int i = 0; i < lines.size();i++)
    {
        pen.setColor(colors[i]);
        pen.setWidth(widths[i]);
        painter.setPen(pen);
        painter.drawLine(lines[i]);
    }
    painter.end();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    end = event->pos();
    QLine line(start,end);
    lines << line;
    if (!eraser)
    {
        colors << color;
        widths << width;
    }
    else
    {
        colors << backgroundColor;
        widths << width;
    }
    start = end;
    update();
}

运行结果:

https://i-blog.csdnimg.cn/direct/cf2b8a3134c64e4c83333bc22ca21836.png

2.追加橡皮擦功能

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    end = event->pos();
    QLine line(start,end);
    lines << line;
    if (!eraser)
    {
        colors << color;
        widths << width;
    }
    else
    {
        colors << backgroundColor;
        widths << width;
    }
    start = end;
    update();
}


void Widget::on_pushButton_5_clicked()
{
    eraser=!eraser;
}

运行效果:

https://i-blog.csdnimg.cn/direct/50d5612a2e724c1cb46ea8f803f16808.gif