Adding QGridLayout to a QVBoxLayout
-
Hello,
I am fairly new to Qt and have a pretty simple question about layouts.
I have a QVBoxLayout that is the main layout for my application. Then I make a new QGridLayout, add some stuff to it, and then I add the QGridLayout to the QVBoxLayout using boxLayout->addLayout(gridLayout).
This works, and makes the things that I added to the QGridLayout appear on my QVBoxLayout, but they are at the top of the page. I can't seem to find out how to reposition the items to where I want them, which is at the bottom of the page.
I hope this is a simple fix, and I am just a bit too tired today. Thanks for any help and ideas.----------EDIT----------
Okay, so I have managed to get the QPushButtons out of the layouts causing all these problems. Now this is what I basically have
button1 = new QPushButton("name1", centralWidget); button2 = new QPushButton("name2", centralWidget); button3 = new QPushButton("name3", centralWidget);
where centralWidget is the centralWidget and child of the QMainWindow.
These 3 buttons all appear in the top left corner, one on top of the other, as expected (?). All I want to do is move them to the bottom and lay them out in a QHBoxLayout. The only problem is, there is some other page/tab taking up the whole bottom of the screen, which is not directly associated with the centralWidgetLayout (to my knowledge?). I want to make enough room for the 3 buttons at the bottom of the page.
I will probably make this a new post and add some pictures tomorrow.
-
Hi @MaxDevI,
Try invoking QVBoxLayout::addStretch() on the vbox before you add the grid layout.
(I don't have the tools to try it myself right now, but that's what I'd try first)
Cheers.
-
Hi @Paul-Colby, thanks for the response. I tried adding stretch but all that did was make more space between the things at the top and the rest of the stuff. The items I want at the bottom are still at the very top.
-
Can you show us your vbox and grid layout code? I'm not understanding what you mean by "rest of the stuff" and "items I want a the bottom" etc. Code will be easier to understand :)
-
Sadly I cannot as per company policies. I can try to mock up a version without any pertinent information or variable names, but I will most likely find a solution before I find the energy to do that XD. Also, the code is very messy and layouts are instantiated everywhere so it is even a bit confusing to me how it is set up. Thanks anyway though!
-
Okay, so I have managed to get the QPushButtons out of the layouts causing all these problems. Now this is what I basically have
button1 = new QPushButton("name1", centralWidget); button2 = new QPushButton("name2", centralWidget); button3 = new QPushButton("name3", centralWidget);
where centralWidget is the centralWidget and child of the QMainWindow.
These 3 buttons all appear in the top left corner, one on top of the other, as expected (?). All I want to do is move them to the bottom and lay them out in a QHBoxLayout. The only problem is, there is some other page/tab taking up the whole bottom of the screen, which is not directly associated with the centralWidgetLayout (to my knowledge?). I want to make enough room for the 3 buttons at the bottom of the page.
I will probably make this a new post and add some pictures tomorrow.
-
@MaxDevI said in Adding QGridLayout to a QVBoxLayout:
These 3 buttons all appear in the top left corner, one on top of the other, as expected (?). All I want to do is move them to the bottom and lay them out in a QHBoxLayout.
Try this:
#include <QHBoxLayout> #include <QPushButton> #include <QVBoxLayout> MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) { // A horizontal box of three buttons. QHBoxLayout * const buttonsBox = new QHBoxLayout(this); buttonsBox->addWidget(new QPushButton("name1")); buttonsBox->addWidget(new QPushButton("name2")); buttonsBox->addWidget(new QPushButton("name3")); // A veritcal box with all the space at the top. QVBoxLayout * const vBox = new QVBoxLayout(this); vBox->addStretch(); // This will expand to all available vertical space. vBox->addLayout(buttonsBox); // This will expand to it's sizeHint height. // Add a central widget, and assign our vBox layout. setCentralWidget(new QWidget); centralWidget()->setLayout(vBox); }
Cheers.