QFormLayout内的QHBoxLayout没有左对齐

时间:2016-09-02 08:31:31

标签: qt

我有以下简单代码:

UeDisplaySettingsWidget::UeDisplaySettingsWidget(QWidget *parent)
    : QWidget(parent)
{
    this->setLayout(new QFormLayout());
    this->ueSetDisplayDimmerSelectorLayout(new QHBoxLayout());

    this->ueSetDisplayDimmerSelectorLabel(new QLabel(tr("Display fade level:"),
                                                     this));

    this->ueInitDisplayDimmerLevelSelector();
    this->ueInitDisplayDimmerTimeoutSelector();

    connect(this->ueDisplayDimmerSelector(),
            &QSlider::valueChanged,
            this,
            &UeDisplaySettingsWidget::ueSlotDisplayDimerSelectorValueChanged);

    this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerSelectorLabel());
    this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerSelector());
    this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerCurrentLevelIndicator());

    dynamic_cast<QFormLayout*>(this->layout())->addItem(this->ueDisplayDimmerSelectorLayout());
    dynamic_cast<QFormLayout*>(this->layout())->addRow(tr("Display fade timeout:"),
                                                       this->ueDisplayDimmerTimeoutSelector());
}   // constructor

产生以下输出: QHBoxLayout problem

现在,为什么this->ueDisplayDimmerSelectorLayout()由[{1}}返回并包含QHBoxLayout文字显示淡入淡出级别:QLabelQSlider未与QLCDNumber的下一行对齐?

1 个答案:

答案 0 :(得分:1)

QFormLayout基本上是一个网格布局,有两列,左栏中有标签,右边有标签。

由于某种原因,你可以在水平布局中淡出标签this-&gt; ueDisplayDimmerSelectorLabel()并为你的第一行和QFormLayout :: addRow使用QFormLayout :: addItem(QLayoutItem *)(const QString&amp;,QWidget * ))第二行。这就是为什么你的DisplayDimmerSlectorLabel与第二列中的小部件一起。没有测试它应该看起来像这样:

connect(this->ueDisplayDimmerSelector(),
        &QSlider::valueChanged,
        this,
        &UeDisplaySettingsWidget::ueSlotDisplayDimerSelectorValueChanged);

this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerSelector());
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerCurrentLevelIndicator());

dynamic_cast<QFormLayout*>(this->layout())->addRow(tr("Display fade level:"), this->ueDisplayDimmerSelectorLayout());
dynamic_cast<QFormLayout*>(this->layout())->addRow(tr("Display fade timeout:"),
                                                   this->ueDisplayDimmerTimeoutSelector());