I want to create a custom size grid of squares where user can choose how many columns and rows are in the gird. The sqaures in the grid must be clickable so that they can change color when user clicks them. The grid is ment for pathfinding visalization.
I thought about using QRect that are stored in some kind of map with coordinates or QGraphicsItem in QGraphicsScene. What is the best QT class for this considering performance because i'm not sure if getting the postion of click in mouse event and then looping through huge grid is the best way?
Thank you in advance.
CodePudding user response:
If I were you, I would just put QPushButtons into a QGridLayout. They are clickable and then you can change the colour accordingly.
QGridLayout *buttonLayout = new QGridLayout(this);
size_t rows = 5, columns = 5; // You can make this configurable.
for (size_t row = 0; row < rows; row) {
for(size column = 0; column < column; column) {
QPushButton *button = new QPushButton;
connect(button, &QPushButton::clicked, this, [this](){/* change colours */});
buttonLayout->addWidget(button, row, column);
}
}
centralWidget->setLayout(buttonLayout);