记录一下QHBoxLayout布局

以前些QBoxLayout的时候,只知道往里面addWidget,QBoxLayout会修改添加进去的widget的size,

1.单独添加控件

    setFixedHeight(30);

    m_pLableText1 = new QLabel(this);
    m_pLableText2 = new QLabel(this);
    m_pButton1 = new QPushButton(this);
    m_pButton2 = new QPushButton(this);
    m_pButton3 = new QPushButton(this);

    m_pButton1->setText("1");
    m_pButton2->setText("2");
    m_pButton3->setText("3");

    //m_pLableText1->setText(u8"text");
    //m_pLableText2->setText(u8"text2");

    QHBoxLayout *pHLayout = new QHBoxLayout(this);
    pHLayout->addWidget(m_pButton1);
    pHLayout->addWidget(m_pButton2);
    pHLayout->addWidget(m_pButton3);
    pHLayout->setContentsMargins(0, 0, 0, 0);//默认有margin,不设置成0可能导致内部控件显示不全

    setLayout(pHLayout);

如下

记录一下QHBoxLayout布局_第1张图片

2.设置大小

    m_pButton1->resize(100, 40);
    m_pButton3->resize(200, 80);

实际上resize没有任何影响,还是如上图一样

记录一下QHBoxLayout布局_第2张图片

如果要设置大小,可以设置strech

    pHLayout->addWidget(m_pButton1, 1);
    pHLayout->addWidget(m_pButton2, 2);
    pHLayout->addWidget(m_pButton3);

3.设置对齐方式

    pHLayout->addWidget(m_pButton1, 1);
    pHLayout->addWidget(m_pButton2, 2, Qt::AlignRight);
    pHLayout->addWidget(m_pButton3);

stretch失效了

记录一下QHBoxLayout布局_第3张图片

同时设置stretch和align

    pHLayout->addWidget(m_pButton1, 1);
    pHLayout->addWidget(m_pButton2, 2, Qt::AlignRight);
    pHLayout->addWidget(m_pButton3, 1, Qt::AlignRight);

stretch失效(大小一样),但是多了间隔

记录一下QHBoxLayout布局_第4张图片

4.大小固定setFixedSize

    m_pButton1->setFixedSize(50, 30);
    m_pButton2->setFixedSize(50, 30);
    m_pButton3->setFixedSize(50, 40);

    QHBoxLayout *pHLayout = new QHBoxLayout(this);
    pHLayout->addWidget(m_pButton1, 1);
    pHLayout->addWidget(m_pButton2, 2, Qt::AlignRight);
    pHLayout->addWidget(m_pButton3, 1, Qt::AlignRight);
    pHLayout->setContentsMargins(0, 0, 0, 0);

大小不会被改变了,不会突然被拉伸,

记录一下QHBoxLayout布局_第5张图片

取消stretch,和align

    m_pButton1->setFixedSize(50, 30);
    m_pButton2->setFixedSize(50, 30);
    m_pButton3->setFixedSize(50, 40);

    QHBoxLayout *pHLayout = new QHBoxLayout(this);
    pHLayout->addWidget(m_pButton1);
    pHLayout->addWidget(m_pButton2);
    pHLayout->addWidget(m_pButton3);
    pHLayout->setContentsMargins(0, 0, 0, 0);

变成正常均匀分布

记录一下QHBoxLayout布局_第6张图片

5.部分控件大小固定

    setFixedHeight(30);

    m_pLableText1 = new QLabel(this);
    m_pLableText2 = new QLabel(this);
    m_pButton1 = new QPushButton(this);
    m_pButton2 = new QPushButton(this);
    m_pButton3 = new QPushButton(this);

    m_pButton1->setText("1");
    m_pButton2->setText("2");
    m_pButton3->setText("3");

    m_pLableText1->setText(u8"text");
    //m_pLableText2->setText(u8"text2");

    m_pButton1->setFixedSize(50, 30);
    m_pButton2->setFixedSize(50, 30);
    m_pButton3->setFixedSize(50, 40);

    QHBoxLayout *pHLayout = new QHBoxLayout(this);
    pHLayout->addWidget(m_pLableText1);
    pHLayout->addWidget(m_pButton1);
    pHLayout->addWidget(m_pButton2);
    pHLayout->addWidget(m_pButton3);
    pHLayout->setContentsMargins(0, 0, 0, 0);

    setLayout(pHLayout);

大小固定的控件集中到一侧了

记录一下QHBoxLayout布局_第7张图片

调整addWidget顺序

    pHLayout->addWidget(m_pButton1);
    pHLayout->addWidget(m_pLableText1);
    pHLayout->addWidget(m_pButton2);
    pHLayout->addWidget(m_pButton3);

大小固定的集中到一侧,由大小不固定的分隔,

记录一下QHBoxLayout布局_第8张图片

再插入一个大小不固定的控件

    m_pLableText1->setText(u8"text");
    m_pLableText2->setText(u8"text2");

    m_pButton1->setFixedSize(50, 30);
    m_pButton2->setFixedSize(50, 30);
    m_pButton3->setFixedSize(50, 40);

    QHBoxLayout *pHLayout = new QHBoxLayout(this);
    pHLayout->addWidget(m_pButton1);
    pHLayout->addWidget(m_pLableText1);//分隔
    pHLayout->addWidget(m_pButton2);
    pHLayout->addWidget(m_pLableText2);//分隔
    pHLayout->addWidget(m_pButton3);
    pHLayout->setContentsMargins(0, 0, 0, 0);

    setLayout(pHLayout);

多一个分隔

记录一下QHBoxLayout布局_第9张图片

没有总结

你可能感兴趣的:(qt)