Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

c++ - Qt5 QPushButton's can't be clicked (?!)

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...