-1

I have an inherited widget game_widget in which I declared 9 QPushButton's that are stored in an array via a method init_ui and a layout widget on which the buttons are supposed to be placed. There is also init_ui function that is called in the constructor. Here are the main elements of the class:

class game_widget : public QWidget
{
    Q_OBJECT
    
    public:
    // The layout  widget for the buttons
    QWidget* gridLayoutWidget = new QWidget(this);

    QPushButton** fields; // Fields list
    QPushButton* field1 = new QPushButton(gridLayoutWidget);
    ...
    QPushButton* field9 = new QPushButton(gridLayoutWidget);
    ...

    private:
    void init_ui();
};

Here is init_ui:

void game_widget::init_ui()
{
    fields = new QPushButton* [9]; // Fields list
    fields[0] = field1;
    ...
    fields[8] = field9;

    ...

    // Preparing layout for the buttons
    gridLayoutWidget->setGeometry(QRect(10, 10, 531, 531));
    QGridLayout* grid_layout = new QGridLayout(gridLayoutWidget);

    // Adding each field to the layout
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
        {
            fields[i * 3 + j]->setMaximumSize(QSize(170, 170));
            fields[i * 3 + j]->setMinimumSize(QSize(170, 170));
            grid_layout->addWidget(fields[i * 3 + j], i, j);
        }
}

Now the thing is that those buttons are not even clickable - not to mention that hovering over them doesn't do anything with them as well, there is no animation. Nothing else about them was changed, so their behavior should be normal, but it isn't. If You have the slightest idea what might be going on, please help.

sweak
  • 1,369
  • 2
  • 6
  • 21

1 Answers1

0

You are creating 9 extra QPushButtons in void game_widget::init_ui(), try the following:

void game_widget::init_ui()
{
    QVector <QPushButton*> fields; // Fields list
    fields[0] << field1;
    ...
    fields[8] << field9;

    ...

    // Preparing layout for the buttons
    gridLayoutWidget->setGeometry(QRect(10, 10, 531, 531));
    QGridLayout* grid_layout = new QGridLayout(gridLayoutWidget);

    // Adding each field to the layout
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
        {
            fields[i * 3 + j]->setMaximumSize(QSize(170, 170));
            fields[i * 3 + j]->setMinimumSize(QSize(170, 170));
            grid_layout->addWidget(fields[i * 3 + j], i, j);
        }
}
Chaitanya
  • 177
  • 2
  • 9