[英]can someone help me modify this code. I'm trying to Display a checkerboard C++ QT
I'm new to Qt and C++. 我是Qt和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.
我可以使用主类中的MyWidget.show()在屏幕上显示小部件。 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.
但是我想做类似Board.show()的事情,并显示属于该类的成员的所有正方形对象(具有高度,宽度和颜色)。 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 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 board.h和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 * square.h和square.cpp * 强文本 *
#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);
}
Your Square
is created as an automatic variable (ie, its lifetime is the scope of the constructor of Board
). 您的
Square
被创建为自动变量(即,其生存期是Board
的构造函数的范围)。 show()
will make a widget visible, as long as the event loop can handle the widget, which is not the case here ( square
will be deleted before the event loop). 只要事件循环可以处理小部件,
show()
将使该小部件可见,在此情况并非如此( square
将在事件循环之前被删除)。
Also, you should add the Q_OBJECT
macro in every class derived from QObject
. 另外, 应该在从
QObject
派生的每个类中添加Q_OBJECT
宏 。 Board
is derived from QWidget
, which is derived from QObject
. Board
源自QWidget
, QWidget
源自QObject
。
So, change your class Board
: 因此,更改您的班级
Board
:
class Square;
class Board : public QWidget
{
Q_OBJECT
public:
Board();
private:
Square* square;
};
the constructor: 构造函数:
Board::Board()
{
square = new Square();
square->show();
}
and the destructor: 和析构函数:
Board::~ Board()
{
delete square;
}
Note: for me, having a Square
class is useless. 注意:对我来说,参加
Square
类是没有用的。 You can paint a grid in the paintEvent
of Board
, it will be faster, consume less memory, and be easier to maintain. 您可以在
Board
的paintEvent
中绘制网格,这将更快,消耗更少的内存并且更易于维护。
EDIT : here is a better method: 编辑 :这是一个更好的方法:
void Board::paintEvent(QPaintEvent* pe)
{
// Initialization
unsigned int numCellX = 8, numCellY = 8;
QRect wRect = rect();
unsigned int cellSizeX = wRect.width() / numCellX;
unsigned int cellSizeY = wRect.height() / numCellY;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// Draw the background. The whole widget is drawed.
painter.setBrush(QColor(0,0,0)); //black
painter.drawRect(wRect);
// Draw the cells which are of the other color
// Note: the grid may not fit in the whole rect, because the size of the widget
// should be a multiple of the size of the cells. If not, a black line at the right
// and at the bottom may appear.
painter.setBrush(QColor(255,255,255)); //white
for(unsigned int j = 0; j < numCellY; j++)
for(unsigned int i = j % 2; i < numCellX; i+=2)
painter.drawRect(i * cellSizeX, j * cellSizeY, cellSizeX, cellSizeY);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.