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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:14:46+00:00 2026-06-08T05:14:46+00:00

I created a class called painter and created the QPainter p in the constructor

  • 0

I created a class called painter and created the QPainter p in the constructor passing the QMainWindow as a parameter.

So in the mainwindow.h i added:

protected:
    void paintEvent(QPaintEvent *e);

private:
    Ui::MainWindow *ui; // Created by the QT
    Painter* p;

In the mainwindow.cpp i added

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    p=new Painter(this);
}

and

void MainWindow::paintEvent(QPaintEvent *e)
{
    p->render(this);
}

Painter.h

#ifndef PAINTER_H
#define PAINTER_H
#include <QtCore>
#include <QtGui>

class Painter
{
private:
    QPainter* painter;
    bool init;

public:
    Painter(QMainWindow* m);
    ~Painter();

    void render(QMainWindow* m);
};

#endif // PAINTER_H

and painter.cpp

#include "painter.h"

Painter::Painter(QMainWindow* m)
{
    painter=new QPainter(m);
    //init=false;
}

void Painter::render(QMainWindow* m)
{
    painter->drawLine(10, 3,123, 909);
}

It doesn’t draw a thing.

If i do

void Painter::render(QMainWindow* m)
{
    painer->begin(m);
    painter->drawLine(10, 3,123, 909);
}

It render the line and after 2-3 seconds it closes

The only way to make it work is to do

void Painter::render(QMainWindow* m)
{
    QPainter p(m);
    p.drawLine(10, 3,123, 909);
}

But it seems stupid initialize the p on every frame, it works like that.
There’s a way to just initialize the screen one time or make the begin work?

Thanks.

  • 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-08T05:14:48+00:00Added an answer on June 8, 2026 at 5:14 am

    Solved it with

    painter->begin(this->m);
    painter->drawLine(10, 2, 1232, 1222);
    painter->end();
    

    It seems stupid to me, but its better than declarate it on every frame.
    If someone have a better idea.

    Thanks

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

Sidebar

Related Questions

I've created a class called SpecialArray and I'd like to customize what sort of
I have created a class called Class1, and in another module, I want to
I've a created a class called Animal as shown below: class Animal{ int eyes,legs;
I have created a class library called AddServiceLibrary in which I have a method
I hava a class called DBhelper, and now I created a new class called
I created a class in C++ called Commands (file name commands.cpp). I've taken that
I have created my own button class called custom_btn. I created it on the
I have a peculiar problem. I created a normal Objective C class called SampleTable.
I have an entity called File, I created a partial class with a property
I have created a factory class called AlarmFactory as such... 1 class AlarmFactory 2

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.