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();
}
运行结果:
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;
}
运行效果: