简体   繁体   English

QT-将小部件添加到水平布局

[英]QT - Adding widgets to horizontal layout

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. 例如,当我添加项目时,它首先放在起点,然后放在结尾,然后一直放在结尾。

添加3个小部件

继续添加

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? 有可能这样做吗?

Add a stretcher to it after you have added all the widgets. 添加所有小部件后,向其添加一个担架。

ui->horizontalLayout->addStretch();

will do. 会做。

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. 在这种情况下,请使用insertWidget()函数添加标签。

Or you can add the spaceritem to the end of your horizontal layout after you've added your label widgets. 或者,您可以在添加标签小部件之后将spaceritem添加到水平布局的末尾。

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);
}

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. 删除小部件之间的多余空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM