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. Vertical layout of custom widgets
Forum Updated to NodeBB v4.3 + New Features

Vertical layout of custom widgets

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 760 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.
  • G Offline
    G Offline
    GCDX
    wrote on 6 Jul 2018, 10:38 last edited by
    #1

    I was trying to find out how to make a layout and have custom widgets vertically down just like the idea this guy did.

    https://stackoverflow.com/questions/47835951/qt-how-to-add-custom-widgets-to-a-vertical-layout-to-occupy-the-least-amount-of

    However, i tried his method and the error i keep getting is QWidget::setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout.

    So just asking, for example i have a custom layout of 1 line edit and one pushbutton horizontally aligned, how do i make 5 of the vertically aligned together as such?
    0_1530873353490_4291520f-d9c3-4b0b-a12e-efef16b82559-image.png

    This is the last code i tried:
    QVBoxLayout* nodeVLayout = new QVBoxLayout;
    QSpacerItem* spacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding);

    for(int i=0;i<5;i++){
    Item *node=new Item;
    nodeVLayout->addWidget(node);
    nodeVLayout->addSpacerItem(spacer);
    }
    setLayout(nodeVLayout);

    R 1 Reply Last reply 6 Jul 2018, 10:39
    0
    • G GCDX
      6 Jul 2018, 10:38

      I was trying to find out how to make a layout and have custom widgets vertically down just like the idea this guy did.

      https://stackoverflow.com/questions/47835951/qt-how-to-add-custom-widgets-to-a-vertical-layout-to-occupy-the-least-amount-of

      However, i tried his method and the error i keep getting is QWidget::setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout.

      So just asking, for example i have a custom layout of 1 line edit and one pushbutton horizontally aligned, how do i make 5 of the vertically aligned together as such?
      0_1530873353490_4291520f-d9c3-4b0b-a12e-efef16b82559-image.png

      This is the last code i tried:
      QVBoxLayout* nodeVLayout = new QVBoxLayout;
      QSpacerItem* spacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding);

      for(int i=0;i<5;i++){
      Item *node=new Item;
      nodeVLayout->addWidget(node);
      nodeVLayout->addSpacerItem(spacer);
      }
      setLayout(nodeVLayout);

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 6 Jul 2018, 10:39 last edited by
      #2

      @GCDX
      the QMainWindow itself should never get a layout assigned. It should rather be it's centralWidget.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • G Offline
        G Offline
        GCDX
        wrote on 6 Jul 2018, 10:40 last edited by
        #3

        @raven-worx how do i add a layout inside the Mainwindow??

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GCDX
          wrote on 6 Jul 2018, 10:41 last edited by
          #4

          i tried ui->centralWidget->setLayout(nodeVLayout); and it also doesnt work

          R 1 Reply Last reply 6 Jul 2018, 10:46
          0
          • G Offline
            G Offline
            GCDX
            wrote on 6 Jul 2018, 10:43 last edited by
            #5

            OH okay nvm i solved it thanks so much, you telling me the central widget helps

            1 Reply Last reply
            0
            • G GCDX
              6 Jul 2018, 10:41

              i tried ui->centralWidget->setLayout(nodeVLayout); and it also doesnt work

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 6 Jul 2018, 10:46 last edited by
              #6

              @GCDX
              because you already assigned a layout to your centralWidget as seen in the tree in your screenshot.
              When you then try to set another layout to an item you already specified via QtDesigner you get this warning.

              So why don't you stick to QtDesigner or QtWidgets (C++) at all?

              Alternatively you can add widgets to an existing layout without assigning a new layout.

              QVBoxLayout* vLayout = qobject_cast<QVBoxLayout*>(ui->centralWidget->layout());
              vLayout->addWidget(...);
              

              But this is error prone when you don't know what you are doing ;)

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2

              1/6

              6 Jul 2018, 10:38

              • Login

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