QT布局管理复习

22 篇文章 0 订阅

头文件: 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void checkFindEnabel(QString);

private:
    QLabel *label;
    QLineEdit *msgInput;
    QCheckBox *caseCheck;
    QCheckBox *fowordCheck;
    QPushButton *findBt;
    QPushButton *closeBt;
};
#endif // WIDGET_H

源文件:

#include "widget.h"

Widget::Widget(QWidget *parent)
    :  QWidget(parent)
{

    label = new QLabel(tr("Find &what"));
    msgInput = new QLineEdit();
    label->setBuddy(msgInput);//设置友好组件

    caseCheck = new QCheckBox(tr("Math &case"));
    fowordCheck = new QCheckBox(tr("Search &backward"));
    findBt = new QPushButton(tr("&find"));
    closeBt = new QPushButton(tr("close"));
    findBt->setEnabled(false);
    findBt->setDefault(true);

    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->addWidget(label);
    topLayout->addWidget(msgInput);

    QVBoxLayout *bottomLayout = new QVBoxLayout;
    bottomLayout->addWidget(caseCheck);
    bottomLayout->addWidget(fowordCheck);

    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->addLayout(topLayout);
    leftLayout->addLayout(bottomLayout);

    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->addWidget(findBt);
    rightLayout->addWidget(closeBt);

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);

    connect(msgInput,SIGNAL(textChanged(QString)),this,SLOT(checkFindEnabel(QString)));
    connect(closeBt,SIGNAL(clicked()),this,SLOT(close()));

    setLayout(mainLayout);//将窗口加入布局
    setFixedHeight(sizeHint().height());//拉伸时固定高度
    setWindowTitle("find msg");



}

Widget::~Widget()
{
}
void Widget::checkFindEnabel(QString text)
{
    findBt->setEnabled(!text.isEmpty());//不为空findBt才能用
}

布局需求: 

 执行:

各大布局方式 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值