QT DAY2

1.思维导图

2.继续完善登录框,当登录成功时,关闭登录界面,跳转到新的界面中

Second.h

#ifndef SECOND_H
#define SECOND_H

#include <QWidget>
#include <QtCore/QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QHBoxLayout>

namespace Ui {
class Second;
}

class Second : public QWidget
{
    Q_OBJECT


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

private slots:
//    void jump_slot();   //跳转对应槽函数

public slots:
    void slot1();
    //void slot2();
private:
    Ui::Second *ui;
};

#endif // SECOND_H

Widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtCore/QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QHBoxLayout>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT


    QLabel *lab1=new QLabel;
      QLabel *lab2=new QLabel;
    QLabel *lab3=new QLabel;
    QLineEdit *edit1=new QLineEdit;
    QLineEdit *edit2=new QLineEdit;
     QPushButton *btn1=new QPushButton;
     QPushButton *btn2=new QPushButton;
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

signals:
    void jump();   //跳转信号

public slots:
    void on_jumpButton_clicked(); 
    void my_slot();


private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

main.cpp

#include "widget.h"
#include "second.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    Second s;

    QObject::connect(&w,&Widget::jump,&s,&Second::slot1);//将跳转槽函数与信号连接
    return a.exec();
}

Second.cpp

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

Second::Second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Second)
{
    ui->setupUi(this);
    qDebug()<<"second size="<<this->size();
    this->resize(350,300);
    qDebug()<<this->windowTitle();
    this->setWindowTitle("进门有惊喜");
    this->setWindowIcon(QIcon("C:\\Users\\Administrator\\Desktop\\icon\\logo.bmp"));
   // this->setScaledContents(true);
    this->setWindowOpacity(0.8);


}

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

void Second::slot1()
{
    this->show();
}

Widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)

{
    qDebug()<<this->size();
    qDebug()<<this->rect().size();
    qDebug()<<this->geometry().size();
    qDebug()<<this->frameGeometry().size();
    qDebug()<<"width:"<<this->width()<<"  height:"<<this->height();
    qDebug()<<"width:"<<this->size().width()<<"    height:"<<this->size().height();

    //设置固定窗口尺寸
    this->setFixedSize(500,400);

    //窗口标题
    qDebug()<<this->windowTitle();  //获取窗口标题
    this->setWindowTitle("牛马聚集地");

    //设置窗口图标
    this->setWindowIcon(QIcon("C:\\Users\\Administrator\\Desktop\\icon\\logo.bmp"));

    //设置透明度
    this->setWindowOpacity(0.8);

    //设置标签
    QLabel *lab1=new QLabel(this);
    lab1->resize(500,100);
    lab1->setAlignment(Qt::AlignCenter);  //垂直和水平全部居中
    lab1->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\icon\\2023725191221.bmp"));  //设置标签位图标
    lab1->setScaledContents(true);  //自适应

    QLabel *lab2=new QLabel(this);//账号图标
    lab2->resize(50,50);
    lab2->move(100,130);
    lab2->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\icon\\username.bmp"));  //设置标签位图标
    lab2->setScaledContents(true);  //自适应

    QLabel *lab3=new QLabel(this);  //密码图标

    lab3->resize(50,50);
    lab3->move(100,200);
    lab3->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\icon\\passwrd.bmp"));  //设置标签位图标
    lab3->setScaledContents(true);  //自适应

    QLineEdit *edit1=new QLineEdit;
         edit1->setParent(this);   //设置父组件
         edit1->resize(230,50);  //重新设置尺寸
         edit1->move(150,130);   //移动
         edit1->setPlaceholderText(":用户名/账号");  //设置占位符
         edit1->setStyleSheet("border:none;"
                     "border-bottom:2px solid black;");

         QLineEdit *edit2=new QLineEdit(this);
              //edit2->setParent;
              edit2->resize(230,50);
              edit2->move(150,200);
              edit2->setEchoMode(QLineEdit::Password);  //设置密文模式
              edit2->setPlaceholderText(":密码");  //设置占位符
              edit2->setStyleSheet("border:none;"
                          "border-bottom:2px solid black;");

              QPushButton *btn1=new QPushButton(this);
                   btn1->setText("登录");     //设置按钮上的文本内容
                   qDebug()<<btn1->text();   //获得按钮上的文本内容

                   btn1->resize(80,50);     //重新设置按钮的大小
                   qDebug()<<btn1->size();

                   btn1->move(150,300);      //移动组件
                   btn1->setIcon(QIcon("C:\\Users\\Administrator\\Desktop\\icon\\login.bmp"));  //设置按钮图标

                   QPushButton *btn2=new QPushButton(this);
                   btn2->setText("取消");     //设置按钮上的文本内容
                   qDebug()<<btn2->text();   //获得按钮上的文本内容

                   btn2->resize(80,50);     //重新设置按钮的大小
                   qDebug()<<btn2->size();

                   btn2->move(300,300);
                   btn2->setIcon(QIcon("C:\\Users\\Administrator\\Desktop\\icon\\cancel.bmp")); //设置按钮图标

                   connect(btn2,SIGNAL(clicked()),this,SLOT(my_slot()));//连接取消按钮
                  
                  //连接登录按钮
                   connect(btn1,&QPushButton::clicked,[&](){

                       if("admin"==edit1->text()&&"123456"==edit2->text())
                       {
                           qDebug()<<"登录成功"<<endl;
                          on_jumpButton_clicked();

                       }else
                       {
                           qDebug()<<"登录失败"<<endl;
                           edit2->clear();
                       }

                   });

}

Widget::~Widget()
{

}

void Widget::my_slot(){

    this->close();
}

 void Widget::on_jumpButton_clicked(){
    emit jump();
     this->close();
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值