Last active
July 27, 2017 07:48
-
-
Save Bktero/c37c90062c1811ea2e810584784425c9 to your computer and use it in GitHub Desktop.
[Qt] Example of QPainter to create a simulator for led panels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LEDPANEL_HPP | |
#define LEDPANEL_HPP | |
#include <QWidget> | |
#include <cassert> | |
#include <QPainter> | |
class LedPanel : public QWidget | |
{ | |
Q_OBJECT | |
public: | |
LedPanel(QWidget *parent = 0) | |
: QWidget(parent) | |
{ | |
for (int x = 0; x < 32; ++x) | |
{ | |
for (int y = 0; y < 8; ++y) | |
{ | |
states[y][x] = false; | |
} | |
} | |
} | |
void on(unsigned int x, unsigned int y) | |
{ | |
assert(x < 32); | |
assert(y < 8); | |
states[y][x] = true; | |
} | |
protected: | |
void paintEvent(QPaintEvent*) override | |
{ | |
QPainter painter(this); | |
// Background | |
QBrush background(QColor(23, 23, 34)); | |
painter.setBrush(background); | |
painter.drawRect(0, 0, width(), height()); | |
// LED | |
int w = width() / 32; | |
int h = height() / 8; | |
for (int x = 0; x < 32; ++x) | |
{ | |
for (int y = 0; y < 8; ++y) | |
{ | |
if (states[y][x]) | |
{ | |
QBrush led(QColor(0, 125, 110)); | |
painter.setBrush(led); | |
QRect rect(x*w, y*h, w, h); | |
painter.drawRect(rect); | |
} | |
} | |
} | |
} | |
private: | |
bool states[8][32]; // 8 lines of 32 LEDs each | |
}; | |
#endif // LEDPANEL_HPP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment