Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
#include
class ChessboardWidget : public QWidget{
public:
explicit ChessboardWidget(QWidget *parent) : QWidget(parent) {
}
void paintEvent(QPaintEvent *event) override{
this->setPalette(Qt::white);
this->setAutoFillBackground(true);
QPainter painter(this);
bool black = true;
int tileSize = std::ceil(this->height() / 8.0);
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
int y = i * tileSize;
int x = j * tileSize;
painter.fillRect(x, y, tileSize, tileSize, black ? Qt::black : Qt::white);
black = !black;
}
black = !black;
}
}
void resizeEvent(QResizeEvent *event) override{
resize(this->size().height(), this->size().height());
QWidget::resizeEvent(event);
}
private:
};
class MainWindow : public QMainWindow {
public:
MainWindow() {
auto *window = new QWidget();
auto *chess = new ChessboardWidget(window);
setCentralWidget(chess);
setWindowTitle("Chessboard Example");
show();
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->setPalette(Qt::red);
this->setAutoFillBackground(true);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow window;
return QApplication::exec();
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... mainwindow