Q:

Class not calling paintEvent method

My application uses two class and therefore has 2 class "files", First class displays buttons etc. Second class draws rectangles and upon a keypress should change the color of certain rectangles(by redrawing them). Second class contains paintEvent method. The program compiles fine however the rectangles do not show, and I make a test method that should exit the app, however that doesnt work either.
First class (one that is called by main.cpp)
 #include "RCcar.h"
    #include "renderArea.h"
    #include <QPushButton>
    #include <QApplication>
    RCcar::RCcar()
    {
        Renderarea = new renderArea;
        Renderarea->test();
        //Renderarea->update();
        exit = new QPushButton("Exit", this);
        exit->setGeometry(410, 440, 80, 50);
        connect(exit, SIGNAL(released()), QApplication::instance(), SLOT(quit()));
        setFixedSize(500, 500);
    }
#pragma once

#include <QWidget>
class QPushButton;
class renderArea;
class RCcar : public QWidget
{
    Q_OBJECT

public:
    RCcar();
private:
    QPushButton *exit;
    renderArea *Renderarea;
};

Second class which is created in class RCcar
#include "renderArea.h"
#include <QPainter>
#include <QApplication>
#include <QPushButton>
renderArea::renderArea(QWidget *parent)
    : QWidget(parent)
{

    setAutoFillBackground(true);
    setFixedSize(40, 40);
    //this->update();
}
void renderArea::paintEvent(QPaintEvent*) {
    QPainter painter(this);
    QRect a = QRect(90, 230, 70, 40);
    QRect s = QRect(215, 230, 70, 40);
    QRect d = QRect(340, 230, 70, 40);
    QRect w = QRect(215, 150, 70, 40);
    painter.setPen(Qt::black);
    painter.drawText(a, Qt::AlignCenter, "a");
    painter.drawText(s, Qt::AlignCenter, "s");
    painter.drawText(d, Qt::AlignCenter, "d");
    painter.drawText(w, Qt::AlignCenter, "w");
    painter.drawRect(a);
    painter.drawRect(s);
    painter.drawRect(d);
    painter.drawRect(w);
}
void renderArea::test() {
    QApplication::instance()->quit();
}
#pragma once
#include <QWidget>
class QPushButton;
class renderArea : public QWidget 
{
    Q_OBJECT
public:
    enum Keys {w,a,s,d};
    renderArea(QWidget *parent=0);
    void test();
protected:
    void paintEvent(QPaintEvent *event) override;
private:
    Keys keys;
    QPushButton *button;
};

What am I doing wrong?
 thanks

A:

In your code there are 2 errors:

Since you are not going to use layouts you must pass it renderArea the widget as parent, so that it is drawn in the latter.
The second is that in the renderArea constructor you are setting a size of 40 * 40, and when you draw in the paintEvent method you are drawing out of that space.

Corrections are passed as parent to this and set an appropriate size.
Renderarea = new renderArea(this);
Renderarea->setGeometry(0, 0, 500, 400);
exit = new QPushButton("Exit", this);
exit->setGeometry(410, 440, 80, 50);
connect(exit, SIGNAL(released()), QApplication::instance(), SLOT(quit()));
setFixedSize(500, 500);

And delete the line setFixedSize(40, 40);:
renderArea::renderArea(QWidget *parent) : QWidget(parent)
{
    setAutoFillBackground(true);
}

Screenshot:

Note: The paintEvent method had been called but drew in an inappropriate space.

