lulz
lulz

Reputation: 91

Class not calling paintEvent method

My application uses two classes and therefore has 2 class "files".

The second class contains a 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 doesn't work either.

First class, which is called in 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?

Upvotes: 0

Views: 194

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

In your code, there are 2 errors:

  1. Since you are not going to use layouts, you must pass renderArea the widget as parent, so that it is drawn in the latter.

  2. 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:

enter image description here

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

Upvotes: 0

Related Questions