Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3997836
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:27:02+00:00 2026-05-20T07:27:02+00:00

I am learning Qt, and I want to draw lines randomly on a widget

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:27:03+00:00Added an answer on May 20, 2026 at 7:27 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been learning MFC these days.I want to draw lines with MoveTo() and
I am a AS3 novice learning PureMVC and want to write code following best
It's not really hard trouble but as I'm learning language I want my code
I'm learning drawing shapes in WPF. I want draw a Path programmatically, not through
I am learning JXTA and want to master it. I downloaded the tutorial programs
I want to start learning HTML and AJAX using a Linux distribution. Can anyone
I want to start learning more about using SWIG and other methods to interface
I learning Perl and I want to create a simple application that gets all
I learning LINQ, and i want to use pending request, but i have this
I currently learning about scrum and want to learn from experienced professionals in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.