Add widgets to a layout problem



  • Hello all,
    I'm trying to make a dialog where a user could dynamically add and remove widgets (in this case probably QLineEdit's).
    I'm not doing this in MainWindow!

    I have two PushButton objects at the top of the dialog, and everything else is left empty.

    So far I have:

    {
        QLabel *label = new QLabel("Test");
        QHBoxLayout *hlayout = new QHBoxLayout();
        QSpacerItem *spacer = new QSpacerItem(40, 20, QSizePolicy::Fixed);
        QLineEdit *lineEdit = new QLineEdit();
        hlayout->addItem(spacer);
        hlayout->addWidget(lineEdit);
        setLayout(hlayout);
    }
    

    Which works on for the first on_addbuton_clicked() call, but for second and any after second it says: QWidget::setLayout: Attempting to set QLayout "" on AddProduct "AddProduct", which already has a layout

    So, I'm not sure what is happening currently. I guess I don't need to call setLayout() every time, but I don't know how to work around it...

    My plan for this application is for user to create as many qlineedits as he wants, and then, after a click on some PushButton, I want a SQLite table to be created and user input in all of those QLineEdit fields to be table column names.

    Also, what do you consider the best way to read all of the QLineEdit fields that are dynamically created?

    Thanks!


  • Lifetime Qt Champion

    Hi,

    Setup the layout in your widgets constructor so you only have to add the newly created in your on_addbuton_clicked method.

    You can use e.g. findChildren to get all the QLinEdits your created.

    Hope it helps



  • Thanks for such a quick reply! It makes sense to do it the way you suggested.

    Now I have done this> I consider it an improvement, but it still isn't working properly.

    addproducts.h
    
    private:
      QHBoxLayout *hlayout;
    
    AddProduct::AddProduct(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::AddProduct)
    {    
        hlayout = new QHBoxLayout;
        QSpacerItem *spacer = new QSpacerItem(40, 20, QSizePolicy::Fixed);
        QLineEdit *lineEdit = new QLineEdit();
        hlayout->addItem(spacer);
        hlayout->addWidget(lineEdit);
        setLayout(hlayout);
    
        ui->setupUi(this);
    }
    
    void AddProduct::on_addbuton_clicked()
    {
        QLineEdit *lineEdit = new QLineEdit();
        hlayout->addWidget(lineEdit);
        setLayout(hlayout);
        hlayout->update();
    }
    

    Do you notice what I'm doing wrong?

    Thanks in advance.


  • Lifetime Qt Champion

    You still call setLayout. Just remove the two last lines of on_addbuton_clicked



  • So, if I just do>

    void AddProduct::on_addbuton_clicked()
    {
        QLineEdit *lineEdit = new QLineEdit();
        hlayout->addWidget(lineEdit);
    }
    

    It doesn't do anything noticeable... One thing I managed to notice is that the first lineedit (created in the constructor) becomes unresponsive... I can input some text into it, but after on_addbuton_clicked() is called that text becomes unavailable to change or delete.


  • Lifetime Qt Champion

    I just saw ui->setupUi(this); so you're doing it wrong: you should add your QLineEdits to the layout you put in your Designer made UI.


Log in to reply
 

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