QT-day2作业

mywnd.h:

#ifndef MYWND_H
#define MYWND_H

#include <QWidget>
#include"second.h"
#include <iostream>
#include <QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>

class MyWnd : public QWidget
{
    Q_OBJECT


signals:
    void jump();       //自定义跳转信号函数


private slots:
    void on_btn1_clicked(QLineEdit *edit1,QLineEdit *edit2);
    void on_btn2_clicked();


public:
    MyWnd(QWidget *parent = nullptr);
    ~MyWnd();



    Second *s1;        //定义另一个界面的指针
};
#endif // MYWND_H

second.h:

#ifndef SECOND_H
#define SECOND_H

#include <QWidget>

namespace Ui {
class Second;
}

class Second : public QWidget
{
    Q_OBJECT


public slots:
    void jump_slot();       //接受跳转信号的槽函数


public:
    explicit Second(QWidget *parent = nullptr);
    ~Second();

private:
    Ui::Second *ui;
};

#endif // SECOND_H

main.cpp:

#include "mywnd.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWnd w;
    w.show();
    return a.exec();
}

mywnd.cpp:

#include "mywnd.h"



MyWnd::MyWnd(QWidget *parent)
    : QWidget(parent)
{
    qDebug()<<this->size();          //获取当前界面的尺寸
    qDebug()<<this->width();          //获取当前组件的宽度
    qDebug()<<this->height();          //获取当前组件的高度
    this->setFixedSize(500,400);       //设置固定尺寸
    qDebug()<<this->windowTitle();        //获取当前组件的窗口标题
    this->setWindowTitle("Widget");        //设置窗口标题
    this->setWindowIcon(QIcon(":/icon/01.png"));         //设置窗口图标
    this->setStyleSheet("background-color:white;");                              //设置样式表
    this->setWindowOpacity(1);             //设置窗口

    //使用无参构造函数,构造一个按钮
    QPushButton *btn1=new QPushButton;          //无参构造
    btn1->setParent(this);                //将当前界面作为父组件
    btn1->setText("登录");              //设置按钮上的文本内容
    qDebug()<<btn1->size();              //获取按钮的尺寸
    btn1->resize(80,40);               //重新设置组件的尺寸
    btn1->setIcon(QIcon(":/icon/login.png"));            //设置图标
    btn1->move(150,320);             //移动组件位置
    btn1->setStyleSheet("background-color:gray;");                         //设置样式表

    //构造一个按钮,构造时直接指定父组件
    QPushButton *btn2=new QPushButton(this);
    btn2->setText("取消");
    btn2->resize(btn1->size());                 //使用其他组件的尺寸给该组件赋值
    btn2->move(btn1->x()+150,btn1->y());          //使用其他组件的位置给该组件定位
    btn2->setIcon(QIcon(":/icon/cancel.png"));            //设置图标
    btn2->setStyleSheet("background-color:gray;");                         //设置样式表

    //实例化一个标签,并指定父组件
    QLabel *lab1=new QLabel(this);
    lab1->resize(500,150);                      //重新设置尺寸
    lab1->setStyleSheet("background-color:gray;");                         //设置样式表
    qDebug()<<lab1->text();                //获取标签文本内容
    //lab1->setAlignment(Qt::AlignCenter);       //文本居中对齐
    lab1->setPixmap(QPixmap(":/icon/logo.png"));        //设置图片
    lab1->setScaledContents(true);                       //设置内容自适应

    //再实例化一个标签,并指定父组件
    QLabel *lab2=new QLabel(this);
    lab2->resize(50,40);                      //重新设置尺寸
    lab2->setStyleSheet("background-color:gray;");                         //设置样式表
    lab2->setPixmap(QPixmap(":/icon/userName.jpg"));        //设置图片
    lab2->setScaledContents(true);                       //设置内容自适应
    lab2->move(100,180);             //移动组件位置

    //再实例化一个标签,并指定父组件
    QLabel *lab3=new QLabel(this);
    lab3->resize(lab2->size());                 //使用其他组件的尺寸给该组件赋值
    lab3->move(lab2->x(),lab2->y()+60);          //使用其他组件的位置给该组件定位
    lab3->setStyleSheet("background-color:gray;");                         //设置样式表
    lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));        //设置图片
    lab3->setScaledContents(true);                       //设置内容自适应

    //构造一个行编辑器对象,并指定父组件
    QLineEdit *edit1=new QLineEdit(this);
    edit1->resize(250,40);                     //重新设置尺寸
    edit1->move(lab2->x()+60,lab2->y());         //移动组件位置
    //edit1->setEchoMode(QLineEdit::Password);       //设置回显模式
    edit1->setMaxLength(6);              //设置文本最大长度
    edit1->setPlaceholderText("用户名");     //设置占位文本
    edit1->setAlignment(Qt::AlignCenter);       //文本居中对齐

    //再构造一个行编辑器对象,并指定父组件
    QLineEdit *edit2=new QLineEdit(this);
    edit2->resize(edit1->size());                   //使用其他组件的尺寸给该组件赋值
    edit2->move(lab3->x()+60,lab3->y());         //移动组件位置
    edit2->setEchoMode(QLineEdit::Password);       //设置回显模式
    edit2->setMaxLength(6);              //设置文本最大长度
    edit2->setPlaceholderText("用户密码");     //设置占位文本
    edit2->setAlignment(Qt::AlignCenter);       //文本居中对齐


    //给另一个界面实例化空间
    s1=new Second;

    //将当前界面的信号,与s1界面的槽函数进行连接
    connect(this,&MyWnd::jump,s1,&Second::jump_slot);


}

MyWnd::~MyWnd()
{
}


//登录按钮对应的槽函数
void MyWnd::on_btn1_clicked(QLineEdit *edit1,QLineEdit *edit2)
{

   if(edit1->text()!="admin"||edit2->text()!="123456")
   {
       int ret=QMessageBox::warning(this,
                                    "错误",
                                    "账号密码不匹配,是否重新登录",
                                    QMessageBox::Ok|QMessageBox::Cancel,
                                    QMessageBox::Ok);
       //对用户选中的按钮进行判断
       if(ret==QMessageBox::Ok)
       {
           edit1->clear();
           edit2->clear();
       }
       else if(ret==QMessageBox::Cancel)
       {
           this->close();
       }
   }
   else
   {
       QMessageBox box(QMessageBox::Information,
                       "登录成功",
                       "登录成功",
                       QMessageBox::Ok,
                       this);
       box.exec();

       emit jump();
       this->hide();
   }
}



//取消按钮对应的槽函数
void MyWnd::on_btn2_clicked()
{
    int ret=QMessageBox::question(this,
                                  "取消",
                                  "是否确定要退出登录",
                                  QMessageBox::Yes|QMessageBox::No,
                                  QMessageBox::No);
    if(ret==QMessageBox::Yes)
    {
        this->close();
    }

}











second.cpp:

#include "second.h"
#include "ui_second.h"

Second::Second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Second)
{
    ui->setupUi(this);
}

Second::~Second()
{
    delete ui;
}


//接受跳转信号对应的槽函数
void Second::jump_slot()
{
    this->show();     //将自己界面进行展示
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值