C++ QT - 将小部件添加到水平布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16427258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QT - Adding widgets to horizontal layout
提问by abby
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?
有可能这样做吗?
回答by Lee White
Add a stretcher to it after you have added all the widgets.
添加完所有小部件后,为其添加一个担架。
ui->horizontalLayout->addStretch();
will do.
会做。
回答by thuga
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.
或者,您可以在添加标签小部件后将间隔项添加到水平布局的末尾。
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);
}
回答by Straticiuc Vicu
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.
setAlignment(Qt::AlignLeft);
应该做的伎俩。您也可以将其用于:setSpacing(0);setContentsMargins(0, 0, 0, 0);
删除小部件之间的额外空间。