Add/Delete Multiple Widgets to QGridLayout Programmatically
-
Hi All,
I want to add multiple Widgets programmatically to a QGridLayout. I have already created the following layout using QT Creator.
There is one Horizontal Layout Frame and another Frame below it without specifying the layout type.
I have a button call Add, when I click it, it is supposed to add some labels and combo box, and Line Edits horizontally. When I click the button again, it is supposed to add another row below, similar to the previous row.
The on button click code does the following:
QGridLayout* specGridLayout = qobject_cast<QGridLayout*>(ui->specFrameLayout->layout()); if(!specGridLayout){ qDebug() << "Failed to qobject_cast."; specGridLayout = new QGridLayout(ui->specFrameLayout); } // Create LineEdit, ComboBox, and two LineEdits QLineEdit* specNameLine = new QLineEdit(this); specNameLine->setFixedWidth(ui->specNameL->width()+20); specNameLine->setAlignment(Qt::AlignCenter); // Apply a style sheet to hide the line edit border and leave only the bottom edge specNameLine->setStyleSheet("border: none; border-bottom: 1px solid black;"); // Connect the combo box QLabel *specTypeLine = new QLabel("", ui->specFrameLayout); specTypeLine->setText(ui->distributionNum->currentText()); specTypeLine->setFixedWidth(ui->specTypeL->width()); specTypeLine->setAlignment(Qt::AlignCenter); QComboBox* rangeTypeCombo = new QComboBox(this); rangeTypeCombo->setFixedWidth(ui->rangeTypeL->width()+10); // Add items to the ComboBox rangeTypeCombo->addItem("Select Range Type"); rangeTypeCombo->addItem("Min"); rangeTypeCombo->addItem("Max"); rangeTypeCombo->addItem("Range");
To add the widgets, I am using addWidget overload function.
specGridLayout->addWidget(specNameLine, 0, 0, 0, 0, Qt::AlignLeft); specGridLayout->addWidget(specTypeLine, 0, 1, 0, 5, Qt::AlignLeft); specGridLayout->addWidget(rangeTypeCombo, 0, 7, 0, 5, Qt::AlignLeft);
I want to add But the results I am getting is not what expected. I am getting the following layout, when I click the button.
As you can see above, I am not able to exactly control the positions to align with the top labels.
Above code adds only one row. I tried adding incrementing rows to add the widgets, but that did not display my widgets in the second row.
Any ideas or suggestions I can do the resolve the issue? Maybe there is a better approach to implement this design?
Thanks a lot!!
-
@Radio1985 The code only adds one row and not multiple, because you don't increment the row number in each "addWidget"-call. In each loop, you have to retrieve the current row count and set it accordingly. Something like this:
int row_count = specGridLayout->rowCount(); specGridLayout->addWidget(specNameLine, row_count , 0, 0, 0, Qt::AlignLeft); specGridLayout->addWidget(specTypeLine, row_count , 1, 0, 5, Qt::AlignLeft); specGridLayout->addWidget(rangeTypeCombo, row_count , 7, 0, 5, Qt::AlignLeft);
Edit: You write that you've tried this already. That should work, though...
-
@Radio1985 said in Add/Delete Multiple Widgets to QGridLayout Programmatically:
But then I get this error below
Show the code...
-
I am using the same code above. Added the additional lines below:
int row_count = specGridLayout->rowCount(); specGridLayout->addWidget(specNameLine, row_count, 0, 0, 0, Qt::AlignLeft); specGridLayout->addWidget(specTypeLine, row_count, 1, 0, 5, Qt::AlignLeft); specGridLayout->addWidget(rangeTypeCombo, row_count, 7, 0, 5, Qt::AlignLeft);
-
@JonB
Thanks, yes it works when I change the spans to 1. I get the following. The issue was the row_number starts with 1, even if I don't have added any rows.Now I have the other issue on aligning them according to the the label positions I have added.
I am not that clear on how the span and positions working.I modify the addWidget as below:
specGridLayout->addWidget(specNameLine, row_count, 0, 1, 1, Qt::AlignLeft); specGridLayout->addWidget(specTypeLine, row_count, 1, 2, 1, Qt::AlignLeft); specGridLayout->addWidget(rangeTypeCombo, row_count, 3, 1, 1, Qt::AlignLeft);
Then I get the following results.
I want to align them according to the label on top.
-
@Radio1985 said in Add/Delete Multiple Widgets to QGridLayout Programmatically:
I want to align them according to the label on top
You added spacers in the first row, but you do not do that for the other rows. That's why they do not align.
-
@Radio1985
I don't understand what exactly is wrong/you want to do from your pictures. Perhaps @jsulm does. But, yes, anything can be added programmatically. In fact if you design to produce a.ui
file that gets processed into aui_....h
file with C++ code to implement everything designed. So "everything is programmatic". You can even examine theui_...h
file if you want to see what code is generated, for you to maybe copy and use elsewhere. -
@Radio1985 said in Add/Delete Multiple Widgets to QGridLayout Programmatically:
Is there is away I can add them programmatically?
Why do you need them? Why don't you simply left align the elements in first row?
-