Inception of Layout



  • 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.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



  • 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


  • Lifetime Qt Champion

    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



  • 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 ?


  • Lifetime Qt Champion

    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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.