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. QLayouts Will Not Cooperate
Forum Updated to NodeBB v4.3 + New Features

QLayouts Will Not Cooperate

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 663 Views 1 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.
  • N Offline
    N Offline
    nob0dy
    wrote on last edited by nob0dy
    #1

    Hi, I'm trying to learn Qt and so I create a new QWidgets Project, here's the code I've written in the constructor of the MainWindow that is automatically generated by the project

        //setup menubar
        this->createMenu();
    
        QTableWidget* aTable = new QTableWidget(0, 2, this);
    
        QVBoxLayout* mainLayout = new QVBoxLayout;
    
        mainLayout->setMenuBar(ui->menuBar);
        mainLayout->addWidget(aTable, 1);
    
        setLayout(mainLayout);
    

    It looks like this with the tablewidget placed on top of the menubar no matter what I do:
    alt text

    I've tried nesting other layouts inside of mainLayout and it does nothing it always looks like that. Can someone tell me what I'm doing wrong?

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to the forums.
      The ui->toolbar is already in a layout (for an std GUI project)
      so not sure why you reassigned it to the main layout?
      ( Did you make a project that is not using a UI file ?)

      Also what Design are you actually after?

      Should the QTableWidget really be up with the toolbar or do you mean to have it in the center ?

      1 Reply Last reply
      3
      • N Offline
        N Offline
        nob0dy
        wrote on last edited by
        #3

        I'm using just the standard QWidget project settings with a .ui file. I would like the QTable widget to be in the center of the MainWindow dialog.

        The design I'm after is a simple utility, the QTableWidget will be underneath the menubar, and underneath the QTableWidget, I'd like a button. I'm writing just a simple /proc/ vfs monitor for linux that will fill the QTableWidget with running processes and when I click a process it will open a child dialog and display it's loaded .so files by parsing /proc/[pid]/maps.

        So, if I nest mainlayout in MainWindow's existing layout it will layout properly or should I just remove the menubar from the existing layout and add it to mainlayout first?

        mrjjM 1 Reply Last reply
        1
        • N nob0dy

          I'm using just the standard QWidget project settings with a .ui file. I would like the QTable widget to be in the center of the MainWindow dialog.

          The design I'm after is a simple utility, the QTableWidget will be underneath the menubar, and underneath the QTableWidget, I'd like a button. I'm writing just a simple /proc/ vfs monitor for linux that will fill the QTableWidget with running processes and when I click a process it will open a child dialog and display it's loaded .so files by parsing /proc/[pid]/maps.

          So, if I nest mainlayout in MainWindow's existing layout it will layout properly or should I just remove the menubar from the existing layout and add it to mainlayout first?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @nob0dy
          Hi
          MainWindow Widget is a bit special as it has a center widget called centralWidget where
          the user can put his stuff. you can just leave the menu and toolbar alone.

          so i think you are looking for something like

          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this); 
          
              QTableWidget *aTable = new QTableWidget(0, 2, this);
          
              QVBoxLayout *mainLayout = new QVBoxLayout;
              mainLayout->addWidget(aTable, 1);
          
              centralWidget()->setLayout(mainLayout); // we apply the layout to this central widget, not MainWindow directly
          
          }
          
          

          alt text

          However, its a requirement to do this from code ?
          The Designer lets you do this very easy.
          Click the MainWindow.ui file
          Drag a QTableWidget to the center of it.
          Right click, on empty area next to the QTableWidget,
          From the layout menu, choose a layout.
          Now drag a PushButton from left to under the QTableWidget.
          Watch the blue line it show. when in place release the mouse button and have the button inserted.

          N 1 Reply Last reply
          3
          • mrjjM mrjj

            @nob0dy
            Hi
            MainWindow Widget is a bit special as it has a center widget called centralWidget where
            the user can put his stuff. you can just leave the menu and toolbar alone.

            so i think you are looking for something like

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this); 
            
                QTableWidget *aTable = new QTableWidget(0, 2, this);
            
                QVBoxLayout *mainLayout = new QVBoxLayout;
                mainLayout->addWidget(aTable, 1);
            
                centralWidget()->setLayout(mainLayout); // we apply the layout to this central widget, not MainWindow directly
            
            }
            
            

            alt text

            However, its a requirement to do this from code ?
            The Designer lets you do this very easy.
            Click the MainWindow.ui file
            Drag a QTableWidget to the center of it.
            Right click, on empty area next to the QTableWidget,
            From the layout menu, choose a layout.
            Now drag a PushButton from left to under the QTableWidget.
            Watch the blue line it show. when in place release the mouse button and have the button inserted.

            N Offline
            N Offline
            nob0dy
            wrote on last edited by
            #5

            @mrjj said in QLayouts Will Not Cooperate:

            However, its a requirement to do this from code ?
            The Designer lets you do this very easy.
            Click the MainWindow.ui file
            Drag a QTableWidget to the center of it.
            Right click, on empty area next to the QTableWidget,
            From the layout menu, choose a layout.
            Now drag a PushButton from left to under the QTableWidget.
            Watch the blue line it show. when in place release the mouse button and have the button inserte

            Hi, thanks a lot. I didn't know that centralwidget in MainWindow was what was causing all the confusion, it's fixed by swapping my line of code:

            this->setLayout(mainLayout);
            

            with your line of code:

            this->centralWidget()->setLayout(mainLayout);
            

            alt text

            On the topic of whether it's required to do this from code, no it's not but I'd also like to learn the layouts manually that way if I were to say not use QtCreator and write code with vim/tmux or whatever else I wouldn't feel crippled without QtCreator's ui designer (which is great btw). I first learned about signals and slots through QT creator's UI and since it made it so easy for me I didn't have a good understanding on how to do signals/slots from code manually.

            mrjjM 1 Reply Last reply
            1
            • N nob0dy

              @mrjj said in QLayouts Will Not Cooperate:

              However, its a requirement to do this from code ?
              The Designer lets you do this very easy.
              Click the MainWindow.ui file
              Drag a QTableWidget to the center of it.
              Right click, on empty area next to the QTableWidget,
              From the layout menu, choose a layout.
              Now drag a PushButton from left to under the QTableWidget.
              Watch the blue line it show. when in place release the mouse button and have the button inserte

              Hi, thanks a lot. I didn't know that centralwidget in MainWindow was what was causing all the confusion, it's fixed by swapping my line of code:

              this->setLayout(mainLayout);
              

              with your line of code:

              this->centralWidget()->setLayout(mainLayout);
              

              alt text

              On the topic of whether it's required to do this from code, no it's not but I'd also like to learn the layouts manually that way if I were to say not use QtCreator and write code with vim/tmux or whatever else I wouldn't feel crippled without QtCreator's ui designer (which is great btw). I first learned about signals and slots through QT creator's UI and since it made it so easy for me I didn't have a good understanding on how to do signals/slots from code manually.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @nob0dy
              Ok good plan.
              Besides the auto connect from Creator is very convenient but not very reliable as it
              breaks if you later rename the widget or the slot.
              So manual connect is the way to go away.

              Also, just in case you didnt notice.

              Anything UI you draw is converted to c++ code.
              (look inside setupUI() )

              So you can use Designer to design something and then take the code and
              reuse.

              1 Reply Last reply
              2

              • Login

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