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.
Taken from
QLayout::addItem()
documentation:So, replacing
addItem()
byaddLayout()
will solve your problem.To answer why
addItem()
has that effect, you can take a look in Qt's source code:Comparing to
addItem()
,addLayout()
callsadoptLayout()
, 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.)