c++ - 为什么 setspacing 属性不起作用?

标签 c++ qt qlineedit qlayout qt5.9

我是 Qt 的新手并正在试验它。我有一个布局,其代码如下:

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;

}

我的输出看起来像这样。 enter image description here

我想减少我的 lineedit 宽度,所以我取消了第 99 行的注释,我的输出如下所示。 enter image description here

setspacing 和 setContentsMargins 属性在这种情况下不起作用。我哪里出错了。Anyhelp 将非常有用。

最佳答案

如果你有一个自动布局,一些东西应该占据空白空间。如果小部件的策略设置为QSizePolicy::Expanding,则小部件将被扩展以填充空白。如果您使小部件的大小固定(QSizePolicy::Fixed)或使用 setMaximum... 限制其大小空白空间将分布在整个布局中。如果这是不可取的,就像你的情况一样,应该在布局中添加一些东西来占用这个空白空间。你有几个选择。我个人会使用 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);
}

这会产生以下结果:

enter image description here

如果您希望空白位于每行的开头,有效地将小部件右对齐,只需将行 layoutRow->addStretch();之前layoutRow->addWidget(label); .要使小部件水平居中,请添加另一个拉伸(stretch),以便在它们之前之后 有一个。与您可以垂直居中小部件的方式相同,添加 layoutMain->addStretch();之前for (int n = 0; n < 3; n++) .

关于c++ - 为什么 setspacing 属性不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51838687/

相关文章:

c++ - 如何在QLineEdit的开头设置修复字符串?

java - 为什么这个float操作在C++和Java中给出不同的结果?

c++ - CUDA 5.5 nvlink undefined reference (继承)

C++构造函数默认值头文件

c++ - 我应该在类里面使用 `this` 吗?

qt - 使用QT AES/ECB/PKCS5Padding解密

c++ - QT按钮更改其大小并剪切其中的文本

c++ - 通过远程桌面运行 qt creator

qt - 将多个信号连接到 Qt 中的单个插槽

qt - 如何将图标放在 QLineEdit 上?