有人能帮我修改这个代码吗。I';我试图显示一个棋盘格C++;QT 我对QT和C++是新的。我正在尝试创建一个棋盘格/棋盘,其中每个方块都是一个对象。我想弄清楚的是如何让每个正方形对象成为我声明的board对象的一部分,并在屏幕上显示它。我可以在主类中使用MyWidget.show()在屏幕上显示小部件。但我想做一些类似Board.show()的事情,并显示属于该类的所有方形对象(具有高度、宽度和颜色)。对于代码,我尝试了什么都没有出现,尽管我能够得到一个方块来显示,但它不在board类中

有人能帮我修改这个代码吗。I';我试图显示一个棋盘格C++;QT 我对QT和C++是新的。我正在尝试创建一个棋盘格/棋盘,其中每个方块都是一个对象。我想弄清楚的是如何让每个正方形对象成为我声明的board对象的一部分,并在屏幕上显示它。我可以在主类中使用MyWidget.show()在屏幕上显示小部件。但我想做一些类似Board.show()的事情,并显示属于该类的所有方形对象(具有高度、宽度和颜色)。对于代码,我尝试了什么都没有出现,尽管我能够得到一个方块来显示,但它不在board类中,c++,qt,C++,Qt,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(); } #包括 #包

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();
}
#包括
#包括“square.h”
#包括“board.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
//正方形;
//square.show();
董事会;
board.show();
返回app.exec();
}
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();
}
\ifndef BOARD\u H
#定义BOARD_H
#包括
类板:公共QWidget
{
公众:
董事会();
};
#endif//BOARD_H
#包括“board.h”
#包括“square.h”
Board::Board()
{
正方形;
//square.show();
}
square.h和square.cpp*强文本*

#ifndef SQUARE\u H
#定义平方
#包括
类Square:公共QWidget
{
公众:
正方形();
受保护的:
无效油漆事件(QPaintEvent*);
};
#endif//SQUARE_H
#包括“square.h”
#包括
Square::Square()
{
QPalette调色板(正方形::调色板());
setColor(backgroundRole(),Qt::white);
设置调色板(调色板);
}
void Square::paintEvent(QPaintEvent*)
{
油漆工(本);
painter.setRenderInt(QPainter::抗锯齿);
画师:退刀(QBrush(#c56c00”);
画家drawRect(10,15,90,60);
}

您的
方块
被创建为一个自动变量(即,它的生命周期是
的构造函数的范围)。
show()

还有,你
Board
源于
QWidget
,QWidget源于
QObject

所以,改变你的课程

class Square;
class Board : public QWidget
{
Q_OBJECT
public:
    Board();
private:
    Square* square;
};
建造商:

Board::Board()
{
    square = new Square();
    square->show();
}
和析构函数:

Board::~ Board()
{
    delete square;
}
注意:对我来说,拥有一个
Square
类是无用的。您可以在
Board
paintEvent
中绘制网格,它将更快、消耗更少的内存,并且更易于维护

编辑:这里有一个更好的方法:

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);
}
void Board::paintEvent(QPaintEvent*pe)
{
//初始化
无符号整数numCellX=8,numCellY=8;
QRect-wRect=rect();
unsigned int-cellSizeX=wRect.width()/numCellX;
unsigned int-cellSizeY=wRect.height()/numCellY;
油漆工(本);
painter.setRenderInt(QPainter::抗锯齿);
//绘制背景。绘制整个小部件。
painter.setBrush(QColor(0,0,0));//黑色
画家:drawRect(wRect);
//绘制其他颜色的单元格
//注意:网格可能不适合整个rect,因为小部件的大小
//应该是单元格大小的倍数。如果不是,则在右侧显示一条黑线
//在底部可能会出现。
画师.镶嵌画(QColor(255255));//白色
for(无符号整数j=0;j
你到底在哪里把方块添加到棋盘上?为什么不使用QGraphicscene?我很困惑。我是QT和C++的新手。修改代码将有助于更好地理解。如何使用QGraphicscene?谢谢上帝保佑。它现在只显示一个正方形。而且它不可玩。析构函数做什么?我把它放在哪里?你只创建了一个
正方形
。。。所以你只看到一个是正常的。我的建议是停止使用
Square
,公猪的绘制完全可以在
Board::paintEvent()
中完成。对于析构函数:请参见。好的,谢谢。您可以创建一个Board::paintEvent()来为我绘制棋盘吗。我真的觉得很难,我正在写。等待2分钟;)上帝保佑你。我应该把它放在哪里D
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);
}