简体   繁体   English

有人可以帮我修改此代码。 我正在尝试显示棋盘格C ++ QT

[英]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源自QWidgetQWidget源自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. 您可以在BoardpaintEvent中绘制网格,这将更快,消耗更少的内存并且更易于维护。

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.

相关问题 有人可以帮助我将此C ++代码转换为C吗? - can someone help me translate this c++ code to c? 有人可以帮我并行化此C ++代码吗? - Can someone help me parallelize this C++ Code? 有人可以帮助我解决我的C ++代码 - someone can help me troubleshoot my c++ code 有人可以帮我理解stmdb,ldmia,以及如何用arm汇编语言实现这个C ++代码? - Can someone help me understand stmdb, ldmia, and how I can go about implementing this C++ code in arm assembly language? 我只是运行简单的 c++ 程序只是为了检查我的 mingW 是否工作正常,但我遇到了这个错误。 有人可以帮我解决吗? - I'm just running the simple c++ program just to check my mingW is working fine or not but I'm having this error. can someone help me to resolve it? Boost C ++代码无法构建,有人可以帮助我进行库链接吗? - Boost C++ code not building, can someone help me with library linking? Stroustrup 的 C++ 图书挑战赛,有人可以帮我理解这段代码吗? - Stroustrup's C++ Book Challenge, Can someone help me understand this code? 有人可以帮我解释一下代码的第二行是什么意思吗? 这是在 C++ - Can someone help me explain what the second line of the code means? This is in C++ 使用此代码时,我总是收到错误消息。 我正在尝试在继承的类中使用构造函数。 C ++ - I keep getting an error when I use this code. I'm trying to use constructors in an inherited class. c++ 有人可以帮助我使用 C++ 中的递归函数吗? - Can someone help me with Recursive Function in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM