简体   繁体   English

Qt5 QPushButton 无法点击 (?!)

[英]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.我有一个继承的小部件game_widget在其中我声明了 9 个QPushButton ,它们通过方法init_ui和一个应该放置按钮的布局小部件存储在数组中。 There is also init_ui function that is called in the constructor.还有在构造函数中调用的init_ui函数。 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 :这是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.如果您对可能发生的事情有丝毫的了解,请提供帮助。

You are creating 9 extra QPushButtons in void game_widget::init_ui() , try the following:您在void game_widget::init_ui()中创建了 9 个额外的QPushButtons ,请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM