Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding QGridLayout to a QVBoxLayout
Forum Updated to NodeBB v4.3 + New Features

Adding QGridLayout to a QVBoxLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 5.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MaxDevI
    wrote on last edited by MaxDevI
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MaxDevI
      wrote on last edited by
      #2

      I have been looking into the insertWidget function and have been able to move the buttons a bit farther down the page. I hope I can correctly format the rest.

      1 Reply Last reply
      0
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        1
        • Paul ColbyP Paul Colby

          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.

          M Offline
          M Offline
          MaxDevI
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Paul ColbyP Offline
            Paul ColbyP Offline
            Paul Colby
            wrote on last edited by
            #5

            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 :)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MaxDevI
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MaxDevI
                wrote on last edited by
                #7

                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.

                Paul ColbyP 1 Reply Last reply
                0
                • M MaxDevI

                  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.

                  Paul ColbyP Offline
                  Paul ColbyP Offline
                  Paul Colby
                  wrote on last edited by Paul Colby
                  #8

                  @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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved