Qt 绘图线
我正在学习 Qt,我想在小部件上随机绘制线条并不断附加新线条。每当在小部件上调用更新时,下面的代码都会在paintEvent中绘制一条随机线,但是如何在调用paintEvent时阻止小部件清除先前绘制的线?有没有办法只附加绘制的对象?
显然,我可以存储所有线条并每次重新绘制它们,但这对于我将要使用此应用程序执行的操作似乎没有必要。
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 2));
painter.drawLine(QPointF(qrand() % 300, qrand() % 300), QPointF(qrand() % 300,qrand() % 300));
}
void MainWindow::on_b_toggleDrawing_triggered()
{
this->update();
}
I am learning Qt, and I want to draw lines randomly on a widget and keep appending the new lines. The code below draws a random line in the paintEvent whenever an update is called on the widget, but how do I stop the widget from clearing the previously drawn line when paintEvent is called? Is there any way to just append drawn objects?
Obviously I could store all the lines and repaint them every time, but that seems unnecessary for what I will be doing with this application.
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 2));
painter.drawLine(QPointF(qrand() % 300, qrand() % 300), QPointF(qrand() % 300,qrand() % 300));
}
void MainWindow::on_b_toggleDrawing_triggered()
{
this->update();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在屏幕外的表面上绘制线条,然后将它们 blit 到绘制事件中的小部件。 QImage 是理想的,因为它是 QPaintDevice 并可以使用 QPainter::drawImage。下面的代码片段假设
this->image
是一个指向与 MainWindow 大小相同的 QImage 的指针。另一种方法是使用 QPainterPath 构建路径。在这种情况下,您只需维护一个 QPainterPath 实例,根据需要添加线条,然后在绘制事件处理程序中绘制路径。我对画家的道路不太熟悉。因此,我不确定性能与以前的方法相比如何。
You could draw the lines on an off-screen surface and blit them to the widget in the paint event. A QImage would be ideal as it is a QPaintDevice and could be drawn using QPainter::drawImage. The snippet below assumes that
this->image
is a pointer to a QImage with the same size as the MainWindow.An alternative would be to build a path using QPainterPath. In that case, you would simply maintain a QPainterPath instance, add lines as needed and then draw the path in the paint event handler. I'm not as familiar with painter paths. So, I'm not sure how the performance compares with the previous approach.
将
autoFillBackground
设置为 false。如果设置的话,它会在调用paintEvent
之前擦除(填充背景颜色)。Set
autoFillBackground
to false. It's erasing (filling with background color) before callingpaintEvent
if set.插入命令
或者,在调用 bool = true 之前
- 保持绘制区域完好无损并允许
新项目将添加到绘画区域。
bool = false - 在绘制项目之前擦除绘制区域。
Or, insert command
prior to calling
bool = true - leaves the paint area intact and allows
new items to be added to the paint area.
bool = false - erases the paint area before drawing items.
每次你想创建下一行时,你可以创建一个
QGraphicsLineItem
(link) 对象并将其添加到QGraphicsScene(链接)
小部件。
请注意,在此解决方案中,您不必费心重新绘制线条,也不必在退出程序时销毁它们,因为 QGraphicsScene 会处理这两个操作。
Each time you want to create the next line you could create a
QGraphicsLineItem
(link) object and add it to aQGraphicsScene
(link) widget.Note that in this solution you have to bother neither about repainting the lines nor about destroying them when quitting the program, because
QGraphicsScene
will take care of both actions.