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. Inception of Layout

Inception of Layout

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.0k 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.
  • E Offline
    E Offline
    EagleWatch
    wrote on last edited by
    #1

    Hello guys !

    First of all, I'm a very beginner in development.

    I have a problem with a button.
    When I click on the button, the goal is to add lines which contains 2 widgets.
    The lines must be add in vertical way.

    The problem is that my lines are added in horizontal way and I don't know what I'm missing.

    Here is my code :

    void mainWindows::newSimu()
    {
        // Creation of Layouts [Main Zone]
        grille = new QGridLayout;
     
        // Creation of Layouts [Second Zone]
        leftVBox = new QVBoxLayout;
        rightVBox = new QVBoxLayout;
        leftHBox = new QHBoxLayout;
        rightHBox = new QHBoxLayout;
     
        grille->addLayout(leftVBox,0, 0, 1, 4);
        grille->addLayout(rightVBox, 0, 5, 1, 4);
     
        // Creation of Widgets
        buttonAddRevenus = new QPushButton;
        buttonAddRevenus->setIcon(QIcon("Images/plus.png"));
        grille->addWidget(buttonAddRevenus,1, 0, 1, 1);
     
        // Revenus Management
        tabNomRevenus = new QVector<QLineEdit*>;
        tabMontantRevenus = new QVector<QDoubleSpinBox*>;
        nomRevenus1 = new QLineEdit;
        nomRevenus1->setPlaceholderText("Nom de la source de revenus :");
     
        montantRevenus1 = new QDoubleSpinBox;
     
        displayRevenusLines(nomRevenus1, montantRevenus1);
     
     
        // Creation of SIGNAL and SLOTS
        connect(buttonAddRevenus, SIGNAL(clicked()), this, SLOT(addRevenusWidgets()));
     
    }
     
    void mainWindows::addRevenusWidgets()
    {
        QLineEdit *newLine = new QLineEdit;
        QDoubleSpinBox *newSpinBox = new QDoubleSpinBox;
     
        newLine->setPlaceholderText("Nom de la source de revenus :");
        displayRevenusLines(newLine, newSpinBox);
    }
     
    void mainWindows::displayRevenusLines(QLineEdit *line, QDoubleSpinBox *spinBox)
    {
        tabNomRevenus->append(line);
        tabMontantRevenus->append(spinBox);
     
        for(int i = 0;i < tabNomRevenus->size(); i++)
        {
            leftHBox->addWidget(tabNomRevenus->at(i));
            leftHBox->addWidget(tabMontantRevenus->at(i));
            leftVBox->addLayout(leftHBox);
     
        }
        zoneCentrale->setLayout(grille);
    }
    

    Moreover, here is what I get each time I click on the button :

    QLayout::addChildLayout: layout "" already has a parent
    QLayout::addChildLayout: layout "" already has a parent
    

    If you don't get something in my code because of the name, tell me. I just translated from French.

    I'm pretty sure I'm missing something stupid but at this way, I really don't get it :(.

    Thank you for you help.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Might be a silly question but:aren't you just passing your Widget to leftHBox ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        EagleWatch
        wrote on last edited by EagleWatch
        #3

        Hi SGaist,
        Thank you.

        I'm passing 2 Widgets in leftHBox because I want those 2 widgets in the same column.
        Then I'm passing the leftHBox in a leftVBox to have this :
        alt text

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Since it's an element that you are re-using several time, you should create a custom widget that contains your QLineEdit and QDoubleSpinBox. Then the only thing you you'll need to do is add them to your leftVbox layout.

          Your current layout problem is here:
          @EagleWatch said in Inception of Layout:

          for(int i = 0;i < tabNomRevenus->size(); i++)
          {
          leftHBox->addWidget(tabNomRevenus->at(i));
          leftHBox->addWidget(tabMontantRevenus->at(i));
          leftVBox->addLayout(leftHBox);

          }
          

          You are re-adding to the same leftHBox layout every time and you are also trying to re-add that leftHBox to your leftVBox

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • E Offline
            E Offline
            EagleWatch
            wrote on last edited by EagleWatch
            #5

            Of course...
            I should see that.

            Thank you for your answer, it helps me so much.

            Here what I did :
            revenusWidgets.h :

            class revenusWidgets : public QWidget
            {
                Q_OBJECT;
            
                public:
                revenusWidgets();
            
                private:
                QLineEdit *nomRevenuLine;
                QDoubleSpinBox *montantRevenuLine;
                QHBoxLayout *HBox;
            };
            

            mainWindows.cpp :

            #include "mainwindows.h"
            #include "revenusWidgets.h"
            [...]
                tabWidgets = new QVector<class revenusWidgets*>;
                revenusWidgets revenus1;
                displayRevenusLines(&revenus1);
            [...]
            }
            
            void mainWindows::addRevenusWidgets()
            {
                revenusWidgets revenus;
                displayRevenusLines(&revenus);
            }
            
            void mainWindows::displayRevenusLines(revenusWidgets *revenuWidget)
            {
                tabWidgets->append(revenuWidget);
            
                for(int i = 0;i < tabWidget->size(); i++)
                {
                    leftVBox->addWidget(tabWidgets->at(i));
                }
                zoneCentrale->setLayout(grille);
            }
            

            revenusWidgets.cpp :

            revenusWidgets::revenusWidgets()
            {
                nomRevenuLine = new QLineEdit;
                montantRevenuLine = new QDoubleSpinBox;
                HBox = new QHBoxLayout;
            
                nomRevenuLine->setPlaceholderText("Nom de la source de revenus : ");
                HBox->addWidget(nomRevenuLine);
                HBox->addWidget(montantRevenuLine);
            
            }
            

            I hope I'm on the right way. But now, my problem is that each time I click on the button to add that custom Widget, my app is crashing.

            Moreover, revenus1 is not displayed. I think I'm missing something with the custon widget class.

            Do you have an idea what's the problem ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You are not initializing the base class and you are not setting the layout on your revenusWidget

              Also, there's no need to allocate your tabWidgets on the heap. QVector handles that for you.

              Another problem you have is that you create revenus1 on the stack, it will get destroyed at the end of the function and you will have a dangling pointer in tabWidgets.

              Before going further, I'd recommend taking a look at Qt's examples and demos. Take the time to do the tutorials.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • E Offline
                E Offline
                EagleWatch
                wrote on last edited by
                #7

                Many thanks to you !

                I was tired and I did it too quickly, that's why I had so many errors..
                My problem is now solved, thanks to you.

                Using a custom widget was the answer of all my troubles.

                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