1> 继续完善登录框,当登录成功时,关闭登录界面,跳转到新的界面中
widget.h
#include "widget.h" //#include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) //, ui(new Ui::Widget) { //ui->setupUi(this); this->setFixedSize(800,650); //设置固定尺寸 qDebug()<<this->windowTitle(); //获取窗口标题 this->setWindowTitle("英雄去超越"); this->setWindowOpacity(0.9); //5.设置窗口图标 this->setWindowIcon(QIcon(":/icon/yxlm.png")); //设置大图 lab1 = new QLabel(this); lab1->resize(800,380); lab1->setPixmap(QPixmap(":/icon/logo.png")); lab1->setScaledContents(true); //使用无参构造函数,构造出一个按钮 btn1 = new QPushButton; btn1->setParent(this); //将当前界面设置成父组件 btn1->setText("登录"); //设置按钮上的文本内容 btn1->resize(80,40); //重新设置组件的大小 btn1->move(500,550); //移动组件 btn1->setStyleSheet("background-color:cyan;");//设置样式表 btn1->setIcon(QIcon(":/icon/2.png")); btn2 = new QPushButton(this); btn2->setText("取消"); //设置按钮上的文本内容 btn2->resize(80,40); //重新设置组件的大小 btn2->move(600,550); //移动组件 btn2->setStyleSheet("background-color:skyblue;");//设置样式表 btn2->setIcon(QIcon(":/icon/1.png")); edit1 = new QLineEdit; edit1->setParent(this); edit1->resize(300,40); edit1-> move(250,450); edit1->setEchoMode((QLineEdit::Password)); edit1->setPlaceholderText("密码"); edit2 = new QLineEdit(this); edit2->resize(300,40); edit2-> move(250,400); edit2->setPlaceholderText("账号"); lab2 = new QLabel; lab2->setParent(this); lab2->setText("账号"); lab2->resize(50,50); lab2->move(200,400); lab3 = new QLabel(this); lab3->setText("密码"); lab3->resize(50,50); lab3->move(200,450); connect(btn1,SIGNAL(clicked()),this,SLOT(btn1_slot())); //qt5 connect(btn2,&QPushButton::clicked,this,&Widget::btn2_slot); //将自定义的信号与槽函数进行连接 connect(this,&Widget::my_signal,[&](){ qDebug()<<"我已经成功连接取消按钮"; btn2->resize(btn2->width()+1,btn2->height()+1); }); //将同一个信号连接到同一个槽 //connect(this,&Widget::my_signal,this,&Widget::close); } Widget::~Widget() { delete ui; } void Widget::my_slot() { this->close(); } void Widget::btn1_slot() { QString userName = edit2->text(); QString pwd = edit1->text(); if(userName == "admin" && pwd == "123456" ) { qDebug()<<"登录成功"; this->close(); emit jump(); this->close(); }else { qDebug()<<"登录失败,请重新登录"; edit1->clear(); } } void Widget::btn2_slot() { static int num = 0; qDebug()<<++num; //发射自定义的信号 emit my_signal(); }
2> 新建一个工程文件,将默认提供的代码加上注释信息
工程管理文件.pro
QT += core gui #表示引入qt所需的类库,如核心库、图形化界面库 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets #如果超过4.0版本,系统会自动加上widgets库 CONFIG += c++11 #该版本的qt支持c++11后的语法 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #管理源文件 SOURCES += \ main.cpp \ widget.cpp #管理头文件 HEADERS += \ widget.h #管理ui文件 FORMS += \ widget.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
3> 思维导图