簡體   English   中英

有人可以幫我修改此代碼。 我正在嘗試顯示棋盤格C ++ QT

[英]can someone help me modify this code. I'm trying to Display a checkerboard C++ QT

我是Qt和C ++的新手。 我正在嘗試創建一個棋盤格/棋盤,其中每個正方形都是一個對象。 我要弄清楚的是如何使每個方形對象都成為我要聲明的板對象的一部分,並將其顯示在屏幕上。 我可以使用主類中的MyWidget.show()在屏幕上顯示小部件。 但是我想做類似Board.show()的事情,並顯示屬於該類的成員的所有正方形對象(具有高度,寬度和顏色)。 使用代碼我沒有嘗試顯示任何東西,盡管我能夠找到一個不在板子課堂上的正方形。

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和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和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);
}

您的Square被創建為自動變量(即,其生存期是Board的構造函數的范圍)。 只要事件循環可以處理小部件, show()將使該小部件可見,在此情況並非如此( square將在事件循環之前被刪除)。

另外, 應該在從QObject派生的每個類中添加Q_OBJECT Board源自QWidgetQWidget源自QObject

因此,更改您的班級Board

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類是沒有用的。 您可以在BoardpaintEvent中繪制網格,這將更快,消耗更少的內存並且更易於維護。

編輯 :這是一個更好的方法:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM