QT之布局

关于QGridlayout 布局
MainWindow.cpp 文件

#include "mainwindow.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    QWidget *widget = new QWidget(this);
    QPushButton *button1 = new QPushButton(tr("One"));
    QPushButton *button2 = new QPushButton(tr("Two"));
    QPushButton *button3 = new QPushButton(tr("Three"));
    QPushButton *button4 = new QPushButton(tr("Four"));
    QPushButton *button5 = new QPushButton(tr("Five"));

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(button1,0,0);
    layout->addWidget(button2,0,1);
    layout->addWidget(button3,1,0,1,2);
    layout->addWidget(button4,2,0);
    layout->addWidget(button5,2,1);

    widget->setLayout(layout); //MainWindow 类不能直接使用布局,所以布局到 widget 上,在调用 setCentralWidget ->设置中心widget
    this->setCentralWidget(widget); 
    //this->setLayout(layout);

}

效果
模仿QQ登陆界面 , 边框问题有待解决。。

#include "mywidget.h"
#include <QtGui>
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{

    this->setWindowTitle("QQ");
    this->setAutoFillBackground(true);
    QPalette pale;
    this->setPalette(pale);
    //构建头像 用户名 密码输入框 等


    QLabel *pImageLabel = new QLabel(this);
    QLineEdit *pUserLineEdit = new QLineEdit(this);
    QLineEdit *pPasswordLineEdit = new QLineEdit(this);
    QCheckBox *pRememberCheckBox = new QCheckBox(this);
    QCheckBox *pAutoLoginCheckBox = new QCheckBox(this);
    QPushButton *pLoginButton = new QPushButton(this);
    QPushButton *pRegisterButton = new QPushButton(this);
    QPushButton *pforgotButton = new QPushButton(this);

    pLoginButton->setFixedHeight(30);
    pUserLineEdit->setFixedWidth(200);

    //设置头像
    QPixmap pixmap("://u=3716988047,3824769988&fm=21&gp=0.jpg");
    pImageLabel->setFixedSize(90,90);
    pImageLabel->setPixmap(pixmap);
    pImageLabel->setScaledContents(true); //设置为自适应大小

    //设置文本
    pUserLineEdit->setPlaceholderText(tr("QQ号码/手机/邮箱"));
    pPasswordLineEdit->setPlaceholderText(tr("密码"));
    pPasswordLineEdit->setEchoMode(QLineEdit::Password);
    pRememberCheckBox->setText(tr("记住密码"));
    pAutoLoginCheckBox->setText(tr("自动登录 "));
    pLoginButton->setText(tr("登陆"));
    pRegisterButton->setText(tr("注册账号"));
    pforgotButton->setText(tr("找回密码"));


    QGridLayout *playout = new QGridLayout;
    playout->addWidget(pImageLabel,0,0,3,1);
    playout->addWidget(pUserLineEdit,0,1,1,2);
    playout->addWidget(pRegisterButton,0,3,1,1);
    playout->addWidget(pPasswordLineEdit,1,1,1,2);
    playout->addWidget(pforgotButton,1,3,1,1);
    playout->addWidget(pRememberCheckBox,2,1,1,1,Qt::AlignLeft|Qt::AlignVCenter); //水平居左
    playout->addWidget(pAutoLoginCheckBox,2,2,1,1,Qt::AlignLeft|Qt::AlignVCenter); //垂直居中
    playout->addWidget(pLoginButton,3,1,1,2);

    playout->setHorizontalSpacing(10); //设置水平间距
    playout->setVerticalSpacing(10);  //设置垂直间距
    playout->setContentsMargins(10,101010);
    setLayout(playout);
}

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值