Qt制作登陆小窗口(八)

重点:运用的控件QMessageBox和函数Accepted和reject。

这次使用了两个类,多添加了一个类,是的能够在登录成功后打开第二个窗口。

所以一共是两个头文件,两个源文件。一个基于类QDialog,一个基于类QWidget。

登录窗口使用用户名:aaa,密码:bbb。

头文件mydialog.h:

#ifndef MYDIALOG_H

#define MYDIALOG_H

#include <QDialog>
#include <QLineEdit>
class MyDialog : public QDialog
{
    Q_OBJECT
public:
parent
    QLineEdit * _username;
    QLineEdit * _password;

signals:

public slots:
    void onOK();
    void onExit();

};

#endif // MYDIALOG_H

头文件:mywidget.h

#ifndef MYWIDGET_H

#define MYWIDGET_H

#include <QWidget>


class Mywidget : public QWidget
{
    Q_OBJECT
public:
parent


signals:

public slots:

};

#endif // MYWIDGET_H

源文件:mydialog.cpp

#include "mydialog.h"

#include <QApplication>
#include <QDebug>
#include "mywidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
MyDialog::MyDialog(QWidget *parent) :
    QDialog(parent)
{
    QLabel *labelusername = new QLabel("username");
    QLabel *labelpassword = new QLabel("password");
    QLineEdit *username = new QLineEdit();
    QLineEdit *password = new QLineEdit();
    password->setEchoMode(QLineEdit::Password);//设置为密码样式
    QPushButton *OK = new QPushButton("Login");
    QPushButton *Exit = new QPushButton("Exit");
clickedonOK
clickedonExit
    _username = username;
    _password = password;

    QVBoxLayout *vbox = new QVBoxLayout();
    QHBoxLayout *hbox = new QHBoxLayout();
    QGridLayout *grid = new QGridLayout();
    QGridLayout *mainLayout = new QGridLayout();


    setLayout(mainLayout);
    mainLayout->setColumnStretch(0,1);//添加列弹簧
    mainLayout->setColumnStretch(2,1);
    mainLayout->setRowStretch(1,0);//添加行弹簧
    mainLayout->setRowStretch(1,2);

    mainLayout->addLayout(vbox,1,1);
    vbox->addLayout(grid);
    vbox->addLayout(hbox);


    grid->addWidget(labelusername,0,0);
    grid->addWidget(labelpassword,1,0);
    grid->addWidget(username,0,1);
    grid->addWidget(password,1,1);

    hbox->addWidget(Exit);
    hbox->addWidget(OK);
}

void MyDialog::onOK()
{
    QString strusername = _username->text();
    QString strpassword = _password->text();
    if(strusername.length() == 0)
    {
        //QMessagebox是弹窗
        QMessageBox::warning(this,"错误","用户名不能为空");//错误提醒窗口
        return;
    }
    if(strpassword.length() == 0)
    {
        QMessageBox::critical(this,"错误","密码不能为空");//致命错误
        return;
        //QMessageBox::information();这两个需要关注返回值,这两个函数返回值为int
        //QMessageBox::question();
    }
    if(strusername == "aaa" && strpassword == "bbb")
    {
        accept();//关闭窗口,并表示结果接受
    }
    else
    {
        QMessageBox::critical(this,"错误","账号密码有误");
        _username->setFocus();//设置焦点
        _username->setSelection(0,20);//从第0个开始,20表示选中后面从第一个开始向后20个所有
    }

}
void MyDialog::onExit()
{
    int ret = QMessageBox::question(this,"请问","真的要退出吗?");
    if(ret == QMessageBox::Yes)
        reject();//关闭窗口,表示退出退出
}

//窗口是show出来的用close关闭
//如果是exec出来的,要用accepted或者rejected

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    MyDialog loginDlg;

    if(loginDlg.exec() == QDialog::Accepted)
    {
        qDebug()<<"before app.exe()";
        Mywidget d;
        d.show();
        return app.exec();
    }
    return -1;
}

源文件:mywidget.cpp

#include "mywidget.h"


Mywidget::Mywidget(QWidget *parent) :
    QWidget(parent)
{
}

转载于:https://my.oschina.net/startstorm/blog/648197

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值