I'm new to Qt and C++. I'm trying to create a checker/chess board where each square is an object. What I'm trying to figure out is how to have each square object be a part of the board object I'm declaring and display that on the screen. I can display a widget on the screen by using MyWidget.show() in the main class. But I want to do something like Board.show() and have all of the square objects that are members of that class(that have a height, width, and color) show up. With the code I tried nothing showed up, although I was able to get a square to show up that was NOT in the board class.
main.cpp
#include <qtgui>
#include "square.h"
#include "board.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//Square square;
//square.show();
Board board;
board.show();
return app.exec();
}
board.h and board.cpp
#ifndef BOARD_H
#define BOARD_H
#include <QWidget>
class Board : public QWidget
{
public:
Board();
};
#endif // BOARD_H
#include "board.h"
#include "square.h"
Board::Board()
{
Square square;
//square.show();
}
square.h and square.cpp*strong text*
#ifndef SQUARE_H
#define SQUARE_H
#include <QWidget>
class Square : public QWidget
{
public:
Square();
protected:
void paintEvent(QPaintEvent *);
};
#endif // SQUARE_H
#include "square.h"
#include <QtGui>
Square::Square()
{
QPalette palette(Square::palette());
palette.setColor(backgroundRole(), Qt::white);
setPalette(palette);
}
void Square::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(QBrush("#c56c00"));
painter.drawRect(10, 15, 90, 60);
}