Qt 绘图线

发布于 2024-10-19 15:50:57 字数 570 浏览 7 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

说谎友 2024-10-26 15:50:57

您可以在屏幕外的表面上绘制线条,然后将它们 blit 到绘制事件中的小部件。 QImage 是理想的,因为它是 QPaintDevice 并可以使用 QPainter::drawImage。下面的代码片段假设 this->image 是一个指向与 MainWindow 大小相同的 QImage 的指针。

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    painter.drawImage(this->rect, *this->image);
}

void MainWindow::on_b_toggleDrawing_triggered()
{
    QPainter painter(this->image);

    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(QPen(Qt::black, 2));
    painter.drawLine(QPointF(qrand() % 300, qrand() % 300),
                     QPointF(qrand() % 300,qrand() % 300));

    this->update();
}

另一种方法是使用 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.

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    painter.drawImage(this->rect, *this->image);
}

void MainWindow::on_b_toggleDrawing_triggered()
{
    QPainter painter(this->image);

    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(QPen(Qt::black, 2));
    painter.drawLine(QPointF(qrand() % 300, qrand() % 300),
                     QPointF(qrand() % 300,qrand() % 300));

    this->update();
}

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.

生生不灭 2024-10-26 15:50:57

autoFillBackground 设置为 false。如果设置的话,它会在调用 paintEvent 之前擦除(填充背景颜色)。

Set autoFillBackground to false. It's erasing (filling with background color) before calling paintEvent if set.

_畞蕅 2024-10-26 15:50:57

插入命令

 this->setAttribute( Qt::WA_NoSystemBackground, bool ) ;

或者,在调用 bool = true 之前

 this->update() ;

- 保持绘制区域完好无损并允许
新项目将添加到绘画区域。

bool = false - 在绘制项目之前擦除绘制区域。

Or, insert command

 this->setAttribute( Qt::WA_NoSystemBackground, bool ) ;

prior to calling

 this->update() ;

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.

帥小哥 2024-10-26 15:50:57

每次你想创建下一行时,你可以创建一个 QGraphicsLineItem (link) 对象并将其添加到 QGraphicsScene(链接)小部件。

请注意,在此解决方案中,您不必费心重新绘制线条,也不必在退出程序时销毁它们,因为 QGraphicsScene 会处理这两个操作。

Each time you want to create the next line you could create a QGraphicsLineItem (link) object and add it to a QGraphicsScene (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文