Qt5 QPushButton不能单击(?!) [英] Qt5 QPushButton's can't be clicked (?!)

查看:581
本文介绍了Qt5 QPushButton不能单击(?!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您曾经遇到过无法单击的按钮吗?

Have You ever had buttons that can't have been clicked?

我有一个继承的小部件 game_widget ,在其中声明了9个 QPushButton ,它们通过方法 init_ui 和一个存储在数组中应该在其上放置按钮的布局小部件.构造函数中还调用了 init_ui 函数.这是该类的主要元素:

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

这里是 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.

推荐答案

您要在 void game_widget :: init_ui()中创建9个额外的 QPushButtons ,请尝试以下操作:

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

这篇关于Qt5 QPushButton不能单击(?!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆