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. Add widgets to a layout problem

Add widgets to a layout problem

Scheduled Pinned Locked Moved General and Desktop
qlayoutqwidgetqlayoutwidget
6 Posts 2 Posters 2.1k 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.
  • J Offline
    J Offline
    jelicicm
    wrote on 14 Sept 2015, 13:03 last edited by jelicicm
    #1

    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!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Sept 2015, 13:16 last edited by
      #2

      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

      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
      • J Offline
        J Offline
        jelicicm
        wrote on 14 Sept 2015, 14:23 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Sept 2015, 14:34 last edited by
          #4

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

          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
          • J Offline
            J Offline
            jelicicm
            wrote on 14 Sept 2015, 14:43 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 14 Sept 2015, 14:46 last edited by
              #6

              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.

              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

              3/6

              14 Sept 2015, 14:23

              • Login

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