Layout problem
-
I am writing a dialog which contains chessboard (64 labels).
The problem is when I try to add pieces to layout in it my board shifts. It does not happen when I do the same on main window of application and that's why I don't get an idea what's going wrong.This is the code which draws the board. The function containing is called before SetPosition (next one) and this one works fine.
@
for (int i = 0; i < 32; ++i) {
if (i % 4 == 0 && i != 0) {
x = 0;
++y;
}
this->WhiteSquare[i].setPixmap(QPixmap("Resources/WhiteSquare.png"));
this->BlackSquare[i].setPixmap(QPixmap("Resources/Black Square.png"));this->WhiteSquare[i].resize(SQUARE_IMAGE_SIZE, SQUARE_IMAGE_SIZE);
this->layout2->addWidget(&WhiteSquare[i], left_top_square_x + SQUARE_IMAGE_SIZE * (y % 2 == 0 ? x : x + 1), left_top_square_y + SQUARE_IMAGE_SIZE * y);square_name[0] = 'a' + (y % 2 == 0 ? x : x + 1);
square_name[1] = '8' - y;this->WhiteSquare[i].set_name(square_name);
this->BlackSquare[i].resize(SQUARE_IMAGE_SIZE, SQUARE_IMAGE_SIZE);
this->layout2->addWidget(&BlackSquare[i], left_top_square_x + SQUARE_IMAGE_SIZE * (y % 2 == 0 ? x + 1 : x), left_top_square_y + SQUARE_IMAGE_SIZE * y);square_name[0] = 'a' + (y % 2 == 0 ? x + 1 : x);
square_name[1] = '8' - y;this->BlackSquare[i].set_name(square_name);
bool b;x += 2;
}
@This is the code which is supposed to pose pieces on the board according to position determined in FEN (does not matter how), but does not work correctly. Pieces are adding to layout2 of the dialog.
@
void NewPositionDialog::SetPosition(const char *FEN)
{for (int i = 0; i < 16; ++i) {
this->WhitePieces[i].clear();
this->BlackPieces[i].clear();this->layout2->removeWidget(&this->WhitePieces[i]);
this->layout2->removeWidget(&this->BlackPieces[i]);
}int white_pieces_labels_counter = 0, black_pieces_labels_counter = 0;
int count = 0;const char pieces[12] = {'R', 'N', 'B', 'Q', 'K', 'P',
'r', 'n', 'b', 'q', 'k', 'p'};
const char *Paths[12] = {"Resources/WhiteRook.png", "Resources/WhiteKnight.png", "Resources/WhiteBishop.png", "Resources/WhiteQueen.png", "Resources/WhiteKing.png", "Resources/WhitePawn.png",
"Resources/BlackRook.png", "Resources/BlackKnight.png", "Resources/BlackBishop.png", "Resources/BlackQueen.png", "Resources/BlackKing.png", "Resources/BlackPawn.png"
};
int x_piece_coord = LEFT_TOP_SQUARE_X, y_piece_coord = LEFT_TOP_SQUARE_Y;
char square_name[3] = {0};
char file = 'a', rank = '8';
while (!isspace(FEN[count])) {
if (FEN[count] == '/')
{
x_piece_coord = LEFT_TOP_SQUARE_X;
y_piece_coord += SQUARE_IMAGE_SIZE;
file = 'a';
--rank;
} else if (isdigit(FEN[count])) {
x_piece_coord += SQUARE_IMAGE_SIZE * (FEN[count] - '0');
file += FEN[count] - '0';
} else {
square_name[0] = file;
square_name[1] = rank;if (FEN[count] == toupper(FEN[count])) {
this->WhitePieces[white_pieces_labels_counter].resize(SQUARE_IMAGE_SIZE, SQUARE_IMAGE_SIZE);
this->WhitePieces[white_pieces_labels_counter].set_name(square_name);
} else {
this->BlackPieces[black_pieces_labels_counter].resize(SQUARE_IMAGE_SIZE, SQUARE_IMAGE_SIZE);
this->BlackPieces[black_pieces_labels_counter].set_name(square_name);
}
for (int i = 0; i < 12; ++i)
if (FEN[count] == pieces[i]) {
if (FEN[count] == toupper(FEN[count]))
this->WhitePieces[white_pieces_labels_counter].setPixmap(QPixmap(Paths[i]));
else
this->BlackPieces[black_pieces_labels_counter].setPixmap(QPixmap(Paths[i]));
break;
}if (FEN[count] == toupper(FEN[count])) {
this->layout2->addWidget(&this->WhitePieces[white_pieces_labels_counter], y_piece_coord, x_piece_coord);
++white_pieces_labels_counter;
} else {
this->layout2->addWidget(&this->BlackPieces[black_pieces_labels_counter], y_piece_coord, x_piece_coord);
++black_pieces_labels_counter;
}
x_piece_coord += SQUARE_IMAGE_SIZE;
++file;
}
++count;
}
}
@Solved it. The problem was that you need to fit labels very precisely one on other (piece on square). Otherwise board starts to shift because there is still a grid layout.