QT网格布局,水平布局,垂直布局设计代码讲解

QT水平布局:头文件<QHBoxLayout>
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QHBoxLayout>
int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    QWidget w;
    w.setWindowTitle("YEDI");
    QPushButton bu("Y");
    QPushButton bu1("e");
    QPushButton bu2("f");
    QLineEdit lineit("olleh");  //设置默认字符
    lineit.setEchoMode(QLineEdit::Normal);
    lineit.setPlaceholderText("hello world");  //设置默认字符(提示)
    QHBoxLayout layout;   //设置水平布局
    layout.addStretch(1); //添加弹簧
    layout.addWidget(&bu); //添加按钮部件
    layout.addWidget(&bu1);
    layout.addWidget(&bu2);
    layout.addWidget(&lineit);
    layout.addStretch(1); //添加弹簧
    w.setLayout(&layout);
    w.show();
    return app.exec();
}

QT垂直布局:头文件<QVBoxLayout>
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    QWidget w;
    w.setWindowTitle("YEDI");
    QPushButton bu("Y");
    QPushButton bu1("e");
    QPushButton bu2("f");
    QLineEdit lineit("olleh");  //设置默认字符
    lineit.setEchoMode(QLineEdit::Normal);
    lineit.setPlaceholderText("hello world");  //设置默认字符(提示)
    QVBoxLayout layout;   //设置垂直布局
    layout.addStretch(1); //添加弹簧
    layout.addWidget(&bu); //添加按钮部件
    layout.addWidget(&bu1);
    layout.addWidget(&bu2);
    layout.addWidget(&lineit);
    layout.addStretch(1); //添加弹簧
    w.setLayout(&layout);
    w.show();
    return app.exec();
}

这两种的布局的缺点在于弹簧不能在上下左右都添加
改进:水平布局和垂直布局相结合
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    QWidget w;
    w.setWindowTitle("YEDI");
    QPushButton bu("Y");
    QPushButton bu1("e");
    QPushButton bu2("f");
    QLineEdit lineit("olleh");  //设置默认字符
    lineit.setEchoMode(QLineEdit::Normal);
    lineit.setPlaceholderText("hello world");  //设置默认字符(提示)
    QVBoxLayout layout;
    layout.addStretch(1);
    layout.addWidget(&bu);
    layout.addWidget(&bu1);
    layout.addWidget(&bu2);
    layout.addWidget(&lineit);
    layout.addStretch(1);
    QHBoxLayout lay;
    lay.addStretch(1);
    lay.addLayout(&layout);
    lay.addStretch(1);
    w.setLayout(&lay);
    w.show();
    return app.exec();
}

网格布局:
#include <QApplication>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QTextBrowser>

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    QWidget w;
    QLineEdit lineEdit;
    QPushButton button("Button");
    QPushButton button1("Button1");
    QPushButton button2("Button2");
    QLabel label("label");
    label.setParent(&w);
    QGridLayout glayout;     //设置网格布局
    glayout.addWidget(&button, 1, 1);   //表示把button按钮放在表格中(1,1)位置
    glayout.addWidget(&button1, 1, 2);
    glayout.addWidget(&button2, 2, 1);
    glayout.addWidget(&label, 2, 2);
    glayout.addWidget(&lineEdit, 3, 1, 1, 2);//表示把lineEdit文本框放在表格中(3,1)位置,大小占一行两列
    glayout.setColumnStretch(0, 1);  //在第0列添加一个弹簧
    glayout.setColumnStretch(3, 1);  //在第3列添加一个弹簧
    glayout.setRowStretch(0, 1);     //在第0行添加一个弹簧
    glayout.setRowStretch(4, 1);     //在第4行添加一个弹簧
    w.setLayout(&glayout);
    w.show();
    return app.exec();
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值