Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
132 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...