Reputation: 678
i have an horizontal layout and i am adding widgets by using
ui->horizontalLayout->addWidget(label);
But adding strategy is not what i want. For example, when i add items, it first puts the start, then puts at the end and keep putting from end.
But, what i want is that, when i add widget to layout, it should be put next to the previous widget. like that ,
is it possible to do that?
Upvotes: 11
Views: 33675
Reputation: 122
setAlignment(Qt::AlignLeft);
should do the trick. Also you can use this with:
setSpacing(0);setContentsMargins(0, 0, 0, 0);
to remove extra space between widgets.
Upvotes: 5
Reputation: 12901
You can add a spacer item, either on the outside of your horizontal layout, or inside your horizontal layout. If you add the spacer item inside the horizontal layout, you have to make sure you add your label widgets before the spacer item. Use the insertWidget() function to add your labels in this case.
Or you can add the spaceritem to the end of your horizontal layout after you've added your label widgets.
Here is an example:
QHBoxLayout *hlayout = new QHBoxLayout;
ui->verticalLayout->addLayout(hlayout);
QSpacerItem *item = new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed);
hlayout->addSpacerItem(item);
for(int i = 0; i < 10; i++)
{
QLabel *label = new QLabel("NOTE");
hlayout->insertWidget(i, label);
}
Upvotes: 9
Reputation: 3709
Add a stretcher to it after you have added all the widgets.
ui->horizontalLayout->addStretch();
will do.
Upvotes: 15