Solved Nested box layouts giving unexpected results
-
Let us do one-by-one. Otherwise it is very difficult to fix your issue.
- Let us take only windowlayout class.
- Constructor- Remove all the code.
- Just create only buttons.
- Add all these buttons to horizontal layout(pBottomLayout).
- this.setLayout(pBottomLayout).
- Create the object of WindowLayout in main.cpp
- Display it.
- If you see all the buttons in the horizontal manner, then your first step is complete.
If are able to do this, then we can get into next step.
Also consider @SGaist suggestion on using the Wizard as well.
-
@dheerendra
I have reduced my code and I have been able to add 4 QPushButtons to a QHBoxLayout and display them horizontally. I have then added the layout to a QFrame widget and it display on the screen at 0,0 of the window. I have then added the frame widget to a QVBoxLayout but it just will not be added to it. when I use pMainLayout->addWidget(pBottomFrame) I expected it to be put inside the vetical layout but nothing happens.#include "windowlayout.h" WindowLayout::WindowLayout(QWidget *pParent) : QWidget(pParent) { pBottomLayout = new QHBoxLayout(); pMainLayout = new QVBoxLayout(); QFrame* pBottomFrame = new QFrame(this); pBottomFrame->setFrameStyle(QFrame::Panel | QFrame::Raised); pBottomFrame->setLineWidth(2); pBackButton = new QPushButton(tr("Back"), this); pNextButton = new QPushButton(tr("Next"), this); pCancelButton = new QPushButton(tr("Cancel"), this); pCertificateButton = new QPushButton(tr("View Certificates"), this); // Add widgets to bottom layout pBottomLayout->addWidget(pCertificateButton); pBottomLayout->addWidget(pBackButton); pBottomLayout->addWidget(pNextButton); pBottomLayout->addWidget(pCancelButton); pBottomFrame->setLayout(pBottomLayout); //pBottomFrame->move(50,50); pMainLayout->addWidget(pBottomFrame); //QLabel* pLabel = new QLabel("Label"); //pMainLayout->addWidget(pLabel); }
-
Hang on. Don't do too many things. Just add buttons, horizontal layout and set layout. Then show it. Does it show the 4 buttons. Confirm this first .
-
Just do this in constructor. Show only WindowLayout object. See how does it go.
pBottomLayout = new QHBoxLayout(); pBackButton = new QPushButton(tr("Back"), this); pNextButton = new QPushButton(tr("Next"), this); pCancelButton = new QPushButton(tr("Cancel"), this); pCertificateButton = new QPushButton(tr("View Certificates"), this); // Add widgets to bottom layout pBottomLayout->addWidget(pCertificateButton); pBottomLayout->addWidget(pBackButton); pBottomLayout->addWidget(pNextButton); pBottomLayout->addWidget(pCancelButton); this->setLayout(pBottomLayout);
-
@dheerendra said in Nested box layouts giving unexpected results:
pBottomLayout = new QHBoxLayout();
pBackButton = new QPushButton(tr("Back"), this);
pNextButton = new QPushButton(tr("Next"), this);
pCancelButton = new QPushButton(tr("Cancel"), this);
pCertificateButton = new QPushButton(tr("View Certificates"), this);// Add widgets to bottom layout
pBottomLayout->addWidget(pCertificateButton);
pBottomLayout->addWidget(pBackButton);
pBottomLayout->addWidget(pNextButton);
pBottomLayout->addWidget(pCancelButton);
this->setLayout(pBottomLayout);I can confirm that this code works and shows on the screen.
-
Now let us add the title in the constructor.
QLabel *title = new QLabel("AaronKelsey"); QVBoxLayout *topLyt = new QVBoxLayout; topLyt->addWidget(title); topLyt->addLayout(pBottomLayout); this->setLayout(topLyt). /* this->setLayout(pBottomLayout)*/
Now your final layout is topLyt. So remove So
this->setLayout(pBottomLayout) & do like the above. -
@dheerendra I have implemented this code and the title displays correctly, will it be difficult to set page 1/2/3 etc as the middle layout in the vertical box layout?
-
Nothing is difficult. We have to apply the concept one by one. Now do the following
QLabel *title = new QLabel("AaronKelsey"); QLabel *centerContent = new QLabel("MidContent"); QVBoxLayout *topLyt = new QVBoxLayout; topLyt->addWidget(title); topLyt->addWidget(centerContent); topLyt->addLayout(pBottomLayout); this->setLayout(topLyt).
Now tell me what happens. With this you should be able to understand how things work.
-
@dheerendra
This is the result so far:I am understanding how this pieces together by adding widgets. I would like each page to use a vertical layer and for that to be added as the center content, how would you go about this?
-
This is where your QStackWidget comes. Now create the instance of WindowLayout objects in your CertTool. Do the rest of the stuff as shown below.
pPageOne = new WindowLayout pPageTwo = new WindowLayout pPageThree = new WindowLayout pStackedWidget = new QStackedWidget(this); // Add pages to stacked widget pStackedWidget->addWidget(pPageOne); pStackedWidget->addWidget(pPageTwo); pStackedWidget->addWidget(pPageThree); // Set Page One as start page pStackedWidget->setCurrentWidget(pPageOne); setCentralWidget(pStackedWidget); // Window settings window()->setWindowTitle("App"); window()->setMinimumSize(600,450); // Connect pages to CertTool to allow changing of stacked widget index connect(pPageOne, SIGNAL(setPage(int)), this, SLOT(setPage(int))); connect(pPageTwo, SIGNAL(setPage(int)), this, SLOT(setPage(int))); connect(pPageThree, SIGNAL(setPage(int)), this, SLOT(setPage(int)));