QPushButton not clickable in second Layout

439 views Asked by At

Someone already asked a similar question, but that person was working with PyQt5 and the Designer, so I couldn't find an answer since I'm coding "from scratch" and in C++.

I'm a beginner with Qt and I'm trying to add two QPushButtons to a window in a specific layout. At first, I tried to just add them to the window to an already existing QVBoxLayout as shown below (this is the window and textBrowser is a QTextBrowser. The buttons have already been correctly initialized).

The buttons work perfectly fine in this case:

auto content = new QWidget(this);
auto windowLayout = new QVBoxLayout(content);
windowLayout->addWidget(textBrowser);

windowLayout->addWidget(button1, 0, Qt::AlignLeft);
windowLayout->addWidget(button2, 0, Qt::AlignRight);

setCentralWidget(content);

However, the Buttons are stacked vertically. I'd like them to be horizontally next to each other, so I tried to add the buttons to a QHBoxLayout, which is then added to the existing QVBoxLayout:

auto content = new QWidget(this);
auto windowLayout = new QVBoxLayout(content);
windowLayout->addWidget(textBrowser);

auto buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(button1, 0, Qt::AlignRight);
buttonLayout->addWidget(button2, 0, Qt::AlignLeft);

windowLayout->addItem(buttonLayout);

setCentralWidget(content);

The buttons appear in the place I'd like them to be, but unfortunately, they are not clickable anymore even though they are not disabled.

Why does this happen? My theory was that a layout might be covering the buttons, so I tried to first add the QHBoxLayout to the QVBoxLayout and then add the buttons, but nothing changed.

Any help or information is greatly appreciated.

1

There are 1 answers

3
Minh On BEST ANSWER

Taken from QLayout::addItem() documentation:

The function addItem() is not usually called in application code. To add a widget to a layout, use the addWidget() function; to add a child layout, use the addLayout() function provided by the relevant QLayout subclass.

So, replacing addItem() by addLayout() will solve your problem.

To answer why addItem() has that effect, you can take a look in Qt's source code:

void QBoxLayout::addLayout(QLayout *layout, int stretch)
{
    insertLayout(-1, layout, stretch);
}

void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch)
{
    Q_D(QBoxLayout);
    if (!d->checkLayout(layout))
        return;
    if (!adoptLayout(layout))
        return;
    if (index < 0)                                // append
        index = d->list.count();
    QBoxLayoutItem *it = new QBoxLayoutItem(layout, stretch);
    d->list.insert(index, it);
    invalidate();
}

void QBoxLayout::addItem(QLayoutItem *item)
{
    Q_D(QBoxLayout);
    QBoxLayoutItem *it = new QBoxLayoutItem(item);
    d->list.append(it);
    invalidate();
}

Comparing to addItem(), addLayout() calls adoptLayout(), which reparents the widgets in the layout to be added. This lets the parent layout control the size of the widgets and determine the events that should be directed to the widgets (mouse hover event, mouse press event, etc.)