开发者

Qt Drawing Lines

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();
}


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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜