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 9003381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:42:18+00:00 2026-06-16T00:42:18+00:00

I am creating widget classes that draw grids using QWidgets paint event. My base

  • 0

I am creating widget classes that draw grids using QWidgets paint event. My base class BaseRenderArea contains this method:

//draws a list of guesses in the grid
void BaseRenderArea::drawGuesses(const QList <GuessPoint> &list, QPainter *painter)
{
    //QPainter painter1(this);

    QPen currentPen = painter->pen();

    painter->setPen(QString("red"));

    for(int i=0;i<list.size();i++)
    {
        GuessPoint gp = list.at(i);

        switch(gp.m_type)
        {
        case GuessPoint::Miss:
            drawMiss(gp.m_row, gp.m_col, painter);
            break;
        case GuessPoint::Dead:
            drawDead(gp.m_row, gp.m_col, painter);
            break;
        case GuessPoint::Hit:
            drawHit(gp.m_row, gp.m_col, painter);
            break;
        default:
            ;
        }
    }

    painter->setPen(currentPen);

}

I am calling this method from the paintEvent of the derived class widget.

//the paint event
//uses the base class to draw the texts and
//the grid
//and then draws the guesses made until now
//as well as the score for each choice of the computer
void DebugRenderArea::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    BaseRenderArea::paintEvent(event);

    //to do: draw guesses
    drawGuesses(&painter);

    //draw scores
    //drawScores(&painter);

}

The drawGuesses method in the derived class is implemented as follows:

//draws the guesses made until this moment
void DebugRenderArea::drawGuesses(QPainter *painter)
{
    QList <GuessPoint> list = m_logic->getListGuesses();
    BaseRenderArea::drawGuesses(list,painter);

}

This approach gives me a segmentation fault in the BaseRenderArea::drawGuesses in the second line painter->setPen….

If I change the BaseRenderArea::drawGuesses with the following code (not reusing the QPainter created in the derived class but creating a new QPainter):

//draws a list of guesses in the grid
void BaseRenderArea::drawGuesses(const QList <GuessPoint> &list, QPainter *painter)
{
    QPainter painter1(this);

    QPen currentPen = painter1.pen();

    painter1.setPen(QString("red"));

    for(int i=0;i<list.size();i++)
    {
        GuessPoint gp = list.at(i);

        switch(gp.m_type)
        {
        case GuessPoint::Miss:
            drawMiss(gp.m_row, gp.m_col, &painter1);
            break;
        case GuessPoint::Dead:
            drawDead(gp.m_row, gp.m_col, &painter1);
            break;
        case GuessPoint::Hit:
            drawHit(gp.m_row, gp.m_col, &painter1);
            break;
        default:
            ;
        }
    }

    painter1.setPen(currentPen);

}

everything works fine. My questions why ? What can I do to use the QPainter created in the paintEvent of the derived class in method of the base class?

  • 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-06-16T00:42:19+00:00Added an answer on June 16, 2026 at 12:42 am

    The documentation says that you can’t have 2 active QPainters on the same paint device.

    So, because BaseRenderArea::paintEvent is probably creating its own QPainter, it might have invalidated the one you created before in DebugRenderArea::paintEvent.

    You could simply reorder the two lines:

    BaseRenderArea::paintEvent(event);
    
    QPainter painter(this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating little GUI for my OpenGL game. I've got two base classes Widget
I'm creating a widget that gets installed on various different sites and I need
I am creating a widget that you can assign a contact to onClick. I
I'm creating a chat widget that will be dropped into CommunityServer. The widget works
I'm creating a custom widget by extending LinearLayout : public class MyWidget extends LinearLayout
I am creating a new Widget using the same structure/style as the other Swing
I am creating a yui3 widget and I keep getting this error: this.constructor.NAME is
I am confused with hundreds of ways of creating JS classes. One says that
Here is my situation: Im creating a widget that site admins can embed in
Everyone This is a question about how to creating a widget in my application.

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.