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

Vertical layout of custom widgets

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 747 Views
  • 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 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);

    raven-worxR 1 Reply Last reply
    0
    • G GCDX

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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on 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 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 last edited by
          #4

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

          raven-worxR 1 Reply Last reply
          0
          • G Offline
            G Offline
            GCDX
            wrote on 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

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

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on 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

              • Login

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