为什么 setspacing 属性不起作用?
Why does the setspacing attribute not work?
我是 Qt 的新手,正在试验 it.I 有一个代码如下所示的布局:
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout *parentLayout = new QVBoxLayout(this);//MainWindow is a QWidget
this->setStyleSheet("background-color:red");
for(int i=0;i<3;i++){
QHBoxLayout* labelLineEdit = f1();
parentLayout->addLayout(labelLineEdit);
}
parentLayout->setContentsMargins(0,0,40,0);
}
QHBoxLayout* MainWindow::f1()
{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Movie");
label->setStyleSheet("background-color:blue;color:white");
label->setMinimumWidth(300);
label->setMaximumWidth(300);
layout->addWidget(label);
QLineEdit *echoLineEdit = new QLineEdit;
//echoLineEdit->setMaximumWidth(120);//line:99
echoLineEdit->setMaximumHeight(50);
echoLineEdit->setMinimumHeight(50);
echoLineEdit->setStyleSheet("background-color:brown");
layout->addWidget(echoLineEdit);
layout->setSpacing(0);
return layout;
}
我的输出是这样的。
我想减少我的 lineedit 宽度,所以我取消了第 99 行的注释,我的输出如下所示。
setspacing 和 setContentsMargins 属性在这种情况下不起作用。我要去哪里wrong.Anyhelp 会很有用
您应该为每一行添加一个间隔项(参考QSpacerItem)
QHBoxLayout* MainWindow::f1()
{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Movie");
label->setStyleSheet("background-color:blue;color:white");
label->setMinimumWidth(300);
label->setMaximumWidth(300);
layout->addWidget(label);
QLineEdit *echoLineEdit = new QLineEdit;
echoLineEdit->setMaximumWidth(120);//line:99
echoLineEdit->setMaximumHeight(50);
echoLineEdit->setMinimumHeight(50);
echoLineEdit->setStyleSheet("background-color:brown");
layout->addWidget(echoLineEdit);
//add spacer here
QSpacerItem * item = new QSpacerItem(100, 1, QSizePolicy::Expanding, QSizePolicy::Fixed);
layout->addItem(item);
layout->setSpacing(0);
return layout;
}
如果你有一个自动布局,一些东西应该占据空白 space。如果 widget(s) 的策略设置为 QSizePolicy::Expanding,则 widget(s) 将被扩展以填充空白。如果您使小部件的大小固定(QSizePolicy::Fixed)或使用 setMaximum...
限制 its/their 大小,空的 space 将分布在整个布局中。如果这是不可取的,就像你的情况一样,应该在布局中添加一些东西来把这个空的 space 起来。你有几个选择。我个人会使用 QBoxLayout::addStretch 而不是 QSpacerItem。这是解决方案,以及对问题代码的一些清理:
#include "MainWindow.h"
#include <QHBoxLayout>
#include <QLineEdit>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
auto *widget = new QWidget(this);
auto *layoutMain = new QVBoxLayout(widget);
for (int n = 0; n < 3; n++)
f1(layoutMain);
layoutMain->setContentsMargins(0, 0, 40, 0);
layoutMain->addStretch();
setCentralWidget(widget);
setStyleSheet("background-color: red");
}
void MainWindow::f1(QVBoxLayout *layoutMain)
{
auto *layoutRow = new QHBoxLayout();
auto *label = new QLabel("Movie", this);
auto *lineEdit = new QLineEdit(this);
label->setStyleSheet("background-color: blue; color: white");
label->setFixedWidth(300);
lineEdit->setMaximumWidth(120);
lineEdit->setFixedHeight(50);
lineEdit->setStyleSheet("background-color: brown");
layoutRow->addWidget(label);
layoutRow->addWidget(lineEdit);
layoutRow->addStretch();
layoutRow->setSpacing(0);
layoutMain->addLayout(layoutRow);
}
这会产生以下结果:
如果您希望空 space 位于每行的开头,有效地将小部件右对齐,只需将行 layoutRow->addStretch();
放在 layoutRow->addWidget(label);
之前。要使小部件水平居中,请添加另一个拉伸,以便在 before 和 after 它们之间有一个拉伸。与在 for (int n = 0; n < 3; n++)
.
之前添加 layoutMain->addStretch();
垂直居中小部件的方式相同
我是 Qt 的新手,正在试验 it.I 有一个代码如下所示的布局:
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout *parentLayout = new QVBoxLayout(this);//MainWindow is a QWidget
this->setStyleSheet("background-color:red");
for(int i=0;i<3;i++){
QHBoxLayout* labelLineEdit = f1();
parentLayout->addLayout(labelLineEdit);
}
parentLayout->setContentsMargins(0,0,40,0);
}
QHBoxLayout* MainWindow::f1()
{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Movie");
label->setStyleSheet("background-color:blue;color:white");
label->setMinimumWidth(300);
label->setMaximumWidth(300);
layout->addWidget(label);
QLineEdit *echoLineEdit = new QLineEdit;
//echoLineEdit->setMaximumWidth(120);//line:99
echoLineEdit->setMaximumHeight(50);
echoLineEdit->setMinimumHeight(50);
echoLineEdit->setStyleSheet("background-color:brown");
layout->addWidget(echoLineEdit);
layout->setSpacing(0);
return layout;
}
我的输出是这样的。
我想减少我的 lineedit 宽度,所以我取消了第 99 行的注释,我的输出如下所示。
setspacing 和 setContentsMargins 属性在这种情况下不起作用。我要去哪里wrong.Anyhelp 会很有用
您应该为每一行添加一个间隔项(参考QSpacerItem)
QHBoxLayout* MainWindow::f1()
{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Movie");
label->setStyleSheet("background-color:blue;color:white");
label->setMinimumWidth(300);
label->setMaximumWidth(300);
layout->addWidget(label);
QLineEdit *echoLineEdit = new QLineEdit;
echoLineEdit->setMaximumWidth(120);//line:99
echoLineEdit->setMaximumHeight(50);
echoLineEdit->setMinimumHeight(50);
echoLineEdit->setStyleSheet("background-color:brown");
layout->addWidget(echoLineEdit);
//add spacer here
QSpacerItem * item = new QSpacerItem(100, 1, QSizePolicy::Expanding, QSizePolicy::Fixed);
layout->addItem(item);
layout->setSpacing(0);
return layout;
}
如果你有一个自动布局,一些东西应该占据空白 space。如果 widget(s) 的策略设置为 QSizePolicy::Expanding,则 widget(s) 将被扩展以填充空白。如果您使小部件的大小固定(QSizePolicy::Fixed)或使用 setMaximum...
限制 its/their 大小,空的 space 将分布在整个布局中。如果这是不可取的,就像你的情况一样,应该在布局中添加一些东西来把这个空的 space 起来。你有几个选择。我个人会使用 QBoxLayout::addStretch 而不是 QSpacerItem。这是解决方案,以及对问题代码的一些清理:
#include "MainWindow.h"
#include <QHBoxLayout>
#include <QLineEdit>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
auto *widget = new QWidget(this);
auto *layoutMain = new QVBoxLayout(widget);
for (int n = 0; n < 3; n++)
f1(layoutMain);
layoutMain->setContentsMargins(0, 0, 40, 0);
layoutMain->addStretch();
setCentralWidget(widget);
setStyleSheet("background-color: red");
}
void MainWindow::f1(QVBoxLayout *layoutMain)
{
auto *layoutRow = new QHBoxLayout();
auto *label = new QLabel("Movie", this);
auto *lineEdit = new QLineEdit(this);
label->setStyleSheet("background-color: blue; color: white");
label->setFixedWidth(300);
lineEdit->setMaximumWidth(120);
lineEdit->setFixedHeight(50);
lineEdit->setStyleSheet("background-color: brown");
layoutRow->addWidget(label);
layoutRow->addWidget(lineEdit);
layoutRow->addStretch();
layoutRow->setSpacing(0);
layoutMain->addLayout(layoutRow);
}
这会产生以下结果:
如果您希望空 space 位于每行的开头,有效地将小部件右对齐,只需将行 layoutRow->addStretch();
放在 layoutRow->addWidget(label);
之前。要使小部件水平居中,请添加另一个拉伸,以便在 before 和 after 它们之间有一个拉伸。与在 for (int n = 0; n < 3; n++)
.
layoutMain->addStretch();
垂直居中小部件的方式相同