I have a problem with painting my squares are the wrong size
Unsolved
General and Desktop
-
I made things bigger so they are easier to see. I want the space between filled in;
I have a widget promoted to a class for the painting.
header:#ifndef RENDERER_H #define RENDERER_H #include <QWidget> #include <QPaintEvent> #include <QPainter> class Renderer : public QWidget { Q_OBJECT public: explicit Renderer(QWidget *parent = nullptr); QSize minimumSizeHint() const Q_DECL_OVERRIDE; QSize sizeHint() const Q_DECL_OVERRIDE; bool isFill[400][400]; protected: void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; signals: }; #endif // RENDERER_H
source:
#include "renderer.h" Renderer::Renderer(QWidget *parent) : QWidget(parent) { for (int y=0;y<400;y++){ for (int x=0;x<400;x++) isFill[x][y] = false; } for (int i=40;i<60;i++) isFill[i][50] = true; } QSize Renderer::minimumSizeHint() const { return QSize(100,100); } QSize Renderer::sizeHint() const { return QSize(400,400); } void Renderer::paintEvent(QPaintEvent *event) { QPainter paint(this); paint.setBrush(QColor(0, 0, 0, 255)); //paint.setRenderHint(QPainter::Antialiasing, true); paint.drawRect(this->rect()); paint.setBrush(QColor(255, 255, 255, 255)); for (int y=0;y<100;y++){ for (int x=0;x<100;x++) if(isFill[x][y]) paint.drawRect(x * 8, y * 8, 8, 8); } }
-
I wanb\t the spaces filled in.
-
drawRect()
uses a brush for the inside of the rectangle and a pen for the border. A pen is by default black so that's why you're seeing the borders. Either set a white pen on the painter or, better yet, usefillRect()
instead ofdrawRect()
, which only fills the rectangle with a brush and has no border.