关于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,10,10,10);
setLayout(playout);
}