QT練手

#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()
{

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章