Drawing A Yellow Rectangle

Qt 5 on Windows

This solution was generated and programmed using Visual Studio Community 2017, Qt 5 Community Edition and Qt Visual Studio Tools.

Unlike the other solutions, this program requires to be split into several source files.

WIP This solution requires additional effort to be displayed in fullscreen; it does not actually change the resolution of the screen it is displayed on; on screens with a higher resolution this causes the viewport to appear in an area at the upper left corner of the screen. I could attempt to re-use the GDI+ solution, but then i would lessen the portability benefit of Qt.

Header file for the class KeyEventReceiver:

/* reqtangle - Draw a yellow rectangle. */
/* KeyEventReceiver.h /
/* Released into the Public Domain on Mar 11 2018 by Tilman Kranz . */

#pragma once
#include "Reqtangle.h"

class KeyEnterReceiver : public QObject {
    Q_OBJECT
protected:
    bool eventFilter(QObject *obj, QEvent *event);
};

Implementation file for the class KeyEventReceiver:

/* reqtangle - Draw a yellow rectangle. */
/* KeyEventReceiver.cpp /
/* Released into the Public Domain on Mar 11 2018 by Tilman Kranz . */

#include "KeyEventReceiver.h"

bool KeyEnterReceiver::eventFilter(QObject *obj, QEvent *event) {
    if (event->type() == QEvent::KeyPress) {
        exit(0);
    }
    else {
       return QObject::eventFilter(obj, event);
    }

    return false;
}

Header file for the class RectangleWidget:

/* reqtangle - Draw a yellow rectangle. */
/* RectangleWidget.h /
/* Released into the Public Domain on Mar 11 2018 by Tilman Kranz . */

#pragma once
#include "Reqtangle.h"

class RectangleWidget : public QWidget {
public:
    RectangleWidget(int, int);
protected:
    void paintEvent(QPaintEvent *);
};

Implementation file for the class RectangleWidget:

/* reqtangle - Draw a yellow rectangle. */
/* RectangleWidget.cpp /
/* Released into the Public Domain on Mar 11 2018 by Tilman Kranz . */

#include <qpainter.h>

#include "RectangleWidget.h"

RectangleWidget::RectangleWidget() {
    QPalette palette(RectangleWidget::palette());

    palette.setColor(backgroundRole(), Qt::white);

    setPalette(palette);
}

void RectangleWidget::paintEvent(QPaintEvent *) {
    QPainter p(this);
    QPainterPath path;

    path.addRect(10, 20, 100, 75);
    p.fillPath(path, Qt::yellow);
    p.drawPath(path);
}

Header file for the class Reqtangle:

/* reqtangle - Draw a yellow rectangle. */
/* Reqtangle.cpp /
/* Released into the Public Domain on Mar 11 2018 by Tilman Kranz . */

#pragma once

#include <QtWidgets/QMainWindow>

#include "ui_Reqtangle.h"

class Reqtangle: public QMainWindow {
    Q_OBJECT
public:
     Reqtangle(QWidget *parent = Q_NULLPTR);
private:
     Ui::ReqtangleClass ui;
};

Implementation file for the class Reqtangle:

/* reqtangle - Draw a yellow rectangle. */
/* Reqtangle.cpp /
/* Released into the Public Domain on Mar 11 2018 by Tilman Kranz . */

#include <qpainter>
#include <qscreen> 
#include "Reqtangle.h"
#include "KeyEventReceiver.h"
#include "RectangleWidget.h"

#define WIDTH 640
#define HEIGHT 480

Reqtangle::Reqtangle(QWidget *parent): QMainWindow(parent) {
    this->setFixedSize(WIDTH, HEIGHT);
    this->setStyleSheet("background-color:black;");
    this->showFullScreen();

    ui.setupUi(this);

    RectangleWidget* my_widget = new RectangleWidget(WIDTH, HEIGHT);

    setCentralWidget(my_widget);

    QList<QToolBar *> allToolBars = this->findChildren<QToolBar *>();
 
    foreach(QToolBar *tb, allToolBars) {
        this->removeToolBar(tb);
    }

    KeyEnterReceiver *key = new KeyEnterReceiver(); this->installEventFilter(key);
}

Main program:

/* reqtangle - Draw a yellow rectangle. */
/* main.cpp /
/* Released into the Public Domain on Mar 11 2018 by Tilman Kranz . */

#include <QtWidgets/QApplication>

#include "Reqtangle.h" 

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    Reqtangle r;

    r.show();

    return a.exec();
}