#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLineEdit>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void enterSlot();
private:
QLineEdit* _userEdit;
QLineEdit* _pwdEdit;
};
#endif // WIDGET_H
#include "widget.h"
#include <QLayout>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setWindowTitle("QT");
this->resize(300,100);
QGridLayout* grid = new QGridLayout(this);
QHBoxLayout* hBox = new QHBoxLayout();
QPushButton* ok = new QPushButton("确定");
QPushButton* cancel = new QPushButton("取消");
QLabel* userLabel = new QLabel("用户");
QLabel* pwdLabel = new QLabel("密码");
_userEdit = new QLineEdit();
_pwdEdit = new QLineEdit();
//一个GridLayout
grid->addWidget(userLabel,1,1,1,1);
grid->addWidget(_userEdit,1,2,1,2);
grid->addWidget(pwdLabel,2,1,1,1);
grid->addWidget(_pwdEdit,2,2,1,2);
//左右上下加弹簧因子
grid->setRowStretch(0,1);
grid->setRowStretch(4,1);
grid->setColumnStretch(0,1);
grid->setColumnStretch(4,1);
grid->addLayout(hBox,3,1,1,3);//把一个QHBoxLayout嵌套在第4行,占3个位置
hBox->addStretch(1);
hBox->addSpacing(80);
//把两个按钮放进去
hBox->addWidget(ok);
hBox->addWidget(cancel);
//设置输入密码隐藏
_pwdEdit->setEchoMode(QLineEdit::Password);
//信号槽
connect(cancel,SIGNAL(clicked()),this,SLOT(close()));
connect(ok,SIGNAL(clicked()),this,SLOT(enterSlot()));
}
//ok按钮处理
void Widget::enterSlot()
{
if(_userEdit->text() == "123")
{
if(_pwdEdit->text() == "123")
qDebug()<<"login success"<<endl;
else
qDebug()<<"error"<<endl;
}
}
Widget::~Widget()
{
}