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. Qt 5.5 add layout to widget without layout
Forum Updated to NodeBB v4.3 + New Features

Qt 5.5 add layout to widget without layout

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 6.2k 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.
  • B Offline
    B Offline
    Borzh
    wrote on last edited by Borzh
    #1

    I set all widgets and layouts using Qt Designer.
    But I leave one QWidget (widgetOccContainer) without any layout. This widget has 2 items: tree view and horizontal layout (treeViewOccGroups & horizontalLayoutOccupations).
    My idea is to add layout later to it. Here is how I did this:

    occupationsLayout = new QHBoxLayout();
    occupationsLayout->addWidget(ui->treeViewOccGroups);
    occupationsLayout->addItem(ui->horizontalLayoutOccupations);
    ui->widgetOccContainer->setLayout(occupationsLayout);
    

    The program crashes on last line.
    If I add widget/item after setLayout(), then the program doesn't crash, but horizontalLayoutOccupations is not working: it is not arranging items properly.

    I tried creating:

    occupationsLayout = new QHBoxLayout(ui->widgetOccContainer);
    

    but it just behaves as if I use setLayout().

    EDIT
    occupationsLayout arranges only child widgets, but not child layouts (why?).

    In other words, if instead of adding horizontalLayoutOccupations I add a widget that has layout set to horizontalLayoutOccupations, this will work.

    Why can't layout be a child to widget?

    JKSHJ 1 Reply Last reply
    0
    • B Borzh

      I set all widgets and layouts using Qt Designer.
      But I leave one QWidget (widgetOccContainer) without any layout. This widget has 2 items: tree view and horizontal layout (treeViewOccGroups & horizontalLayoutOccupations).
      My idea is to add layout later to it. Here is how I did this:

      occupationsLayout = new QHBoxLayout();
      occupationsLayout->addWidget(ui->treeViewOccGroups);
      occupationsLayout->addItem(ui->horizontalLayoutOccupations);
      ui->widgetOccContainer->setLayout(occupationsLayout);
      

      The program crashes on last line.
      If I add widget/item after setLayout(), then the program doesn't crash, but horizontalLayoutOccupations is not working: it is not arranging items properly.

      I tried creating:

      occupationsLayout = new QHBoxLayout(ui->widgetOccContainer);
      

      but it just behaves as if I use setLayout().

      EDIT
      occupationsLayout arranges only child widgets, but not child layouts (why?).

      In other words, if instead of adding horizontalLayoutOccupations I add a widget that has layout set to horizontalLayoutOccupations, this will work.

      Why can't layout be a child to widget?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi @Borzh,

      I don't know exactly what's wrong, but you can try a few things.

      But I leave one QWidget (widgetOccContainer) without any layout. This widget has 2 items: tree view and horizontal layout (treeViewOccGroups & horizontalLayoutOccupations).

      Since you want to add the layout later in C++, perhaps you can create treeViewOccGroups and horizontalLayoutOccupations in C++ too.

      My idea is to add layout later to it. Here is how I did this:

      occupationsLayout->addItem(ui->horizontalLayoutOccupations);
      

      I don't think you should call addItem() directly. Try addLayout() instead.

      If I add widget/item after setLayout(), then the program doesn't crash, but the layout is not working: it is not arranging items properly.

      Do you mean occupationsLayout is not working, or horizontalLayoutOccupations is not working?

      ui->widgetOccContainer->setLayout(occupationsLayout);
      

      The program crashes on last line.

      Note: If you have a memory corruption bug in your code, then your program might crash at a line that is not related to your real problem.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Borzh
        wrote on last edited by Borzh
        #3

        Thanks for your answer. The occupationsLayout is really a custom layout, I just put QHBoxLayout as example, it doesn't work either. I mean occupationsLayout is not working, it is not resizing horizontalLayoutOccupations, although does resize treeViewOccGroups.
        addLayout() doesn't help.
        I don't think it is a memory corruption problem, because those are first lines of code in mainWindow constructor.
        Note that treeViewOccGroups and horizontalLayoutOccupations already are children of widgetOccContainer, maybe qt somehow tries to reparent them?

        The idea is to set all needed values of widgets by Qt Designer... I believe if I create everything from scratch it will work, but I would like to understand why this program is not working.

        JKSHJ 1 Reply Last reply
        0
        • G Offline
          G Offline
          Gres
          wrote on last edited by
          #4

          You can try to add empty layout in Designer (add layout and remove items from it) and in code just use it

          ui->widgetOccContainer->layout->addWidget(ui->treeViewOccGroups);
          ...
          
          1 Reply Last reply
          0
          • B Offline
            B Offline
            Borzh
            wrote on last edited by
            #5

            Ok I think I understood the issue at least.

            When you add a layout as a child of widget, this layout is not resized when widget is resized (why?). But if instead of layout you add another widget that has that layout, it will work!

            But I still leave the thread opened modifying the question.

            1 Reply Last reply
            0
            • B Borzh

              Thanks for your answer. The occupationsLayout is really a custom layout, I just put QHBoxLayout as example, it doesn't work either. I mean occupationsLayout is not working, it is not resizing horizontalLayoutOccupations, although does resize treeViewOccGroups.
              addLayout() doesn't help.
              I don't think it is a memory corruption problem, because those are first lines of code in mainWindow constructor.
              Note that treeViewOccGroups and horizontalLayoutOccupations already are children of widgetOccContainer, maybe qt somehow tries to reparent them?

              The idea is to set all needed values of widgets by Qt Designer... I believe if I create everything from scratch it will work, but I would like to understand why this program is not working.

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @Borzh said:

              The occupationsLayout is really a custom layout

              Do you mean you subclassed QLayout to create occupationsLayout? Can you show us the code?

              Note that treeViewOccGroups and horizontalLayoutOccupations already are children of widgetOccContainer, maybe qt somehow tries to reparent them?

              When an object is added to a layout, that object gets reparented to the layout's parent. However, since your layout is attached to widgetOccContainer, then treeViewOccGroups and horizontalLayoutOccupations should not change parents.

              You can investigate yourself like this:

              // If the object has a non-empty objectName, then qDebug will print that name
              qDebug() << ui->treeViewOccGroups->parent();
              

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              B 1 Reply Last reply
              0
              • JKSHJ JKSH

                @Borzh said:

                The occupationsLayout is really a custom layout

                Do you mean you subclassed QLayout to create occupationsLayout? Can you show us the code?

                Note that treeViewOccGroups and horizontalLayoutOccupations already are children of widgetOccContainer, maybe qt somehow tries to reparent them?

                When an object is added to a layout, that object gets reparented to the layout's parent. However, since your layout is attached to widgetOccContainer, then treeViewOccGroups and horizontalLayoutOccupations should not change parents.

                You can investigate yourself like this:

                // If the object has a non-empty objectName, then qDebug will print that name
                qDebug() << ui->treeViewOccGroups->parent();
                
                B Offline
                B Offline
                Borzh
                wrote on last edited by
                #7

                @JKSH said:

                Do you mean you subclassed QLayout to create occupationsLayout? Can you show us the code?

                It is just a QCardLayout in Qt examples (I use it to switch from one widget to another; in this case I switch from QTreeView "occupation groups" to layout that contains 2 QTreeViews: "occupation industry" and "occupation").

                When an object is added to a layout, that object gets reparented to the layout's parent. However, since your layout is attached to widgetOccContainer, then treeViewOccGroups and horizontalLayoutOccupations should not change parents.

                Yes, I agree, they shouldn't, and debugging the program as you indicated, they don't.
                Anyway, the problem was solved adding instead of layout -> widget that contains that layout. Strangely, but works.

                JKSHJ 1 Reply Last reply
                0
                • B Borzh

                  @JKSH said:

                  Do you mean you subclassed QLayout to create occupationsLayout? Can you show us the code?

                  It is just a QCardLayout in Qt examples (I use it to switch from one widget to another; in this case I switch from QTreeView "occupation groups" to layout that contains 2 QTreeViews: "occupation industry" and "occupation").

                  When an object is added to a layout, that object gets reparented to the layout's parent. However, since your layout is attached to widgetOccContainer, then treeViewOccGroups and horizontalLayoutOccupations should not change parents.

                  Yes, I agree, they shouldn't, and debugging the program as you indicated, they don't.
                  Anyway, the problem was solved adding instead of layout -> widget that contains that layout. Strangely, but works.

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @Borzh said:

                  It is just a QCardLayout in Qt examples (I use it to switch from one widget to another; in this case I switch from QTreeView "occupation groups" to layout that contains 2 QTreeViews: "occupation industry" and "occupation").

                  You mean this one?: http://doc.qt.io/qt-5/layout.html#how-to-write-a-custom-layout-manager That implementation is incomplete (see "Further Notes"). Perhaps the card layout is missing the code required to manage a sub-layouts properly (I haven't investigated this in detail).

                  But anyway, I'm glad to hear that you have a working solution!

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  B 1 Reply Last reply
                  0
                  • JKSHJ JKSH

                    @Borzh said:

                    It is just a QCardLayout in Qt examples (I use it to switch from one widget to another; in this case I switch from QTreeView "occupation groups" to layout that contains 2 QTreeViews: "occupation industry" and "occupation").

                    You mean this one?: http://doc.qt.io/qt-5/layout.html#how-to-write-a-custom-layout-manager That implementation is incomplete (see "Further Notes"). Perhaps the card layout is missing the code required to manage a sub-layouts properly (I haven't investigated this in detail).

                    But anyway, I'm glad to hear that you have a working solution!

                    B Offline
                    B Offline
                    Borzh
                    wrote on last edited by
                    #9

                    @JKSH said:

                    You mean this one?: http://doc.qt.io/qt-5/layout.html#how-to-write-a-custom-layout-manager

                    Correct, that one. But in the code I tried replacing it with QHBoxLayout without luck, layout wasn't managing sub-layout properly. I suppose QHBoxLayout has complete implementation.

                    But anyway, I'm glad to hear that you have a working solution!

                    Thanks for your help. Later I will fight a little more with layouts and if I find a solution, will post it here.

                    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